Login   Register  
PHP Classes
elePHPant
Icontem

File: tests/NinjaMutex/Mock/MockMemcache.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Kamil Dziedzic  >  Ninja Mutex  >  tests/NinjaMutex/Mock/MockMemcache.php  >  Download  
File: tests/NinjaMutex/Mock/MockMemcache.php
Role: Unit test script
Content type: text/plain
Description: Test
Class: Ninja Mutex
Lock resources to prevent simultaneous accesses
Author: By
Last change: Large cleanup + reintroduce Lock __destructor() test
For redis and memcache(d)
Date: 2 months ago
Size: 1,758 bytes
 

Contents

Class file image Download
<?php
/**
 * This file is part of ninja-mutex.
 *
 * (C) Kamil Dziedzic <arvenil@klecza.pl>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace NinjaMutex\Mock;

use 
Memcache;

/**
 * Mock memcache to mimic mutex functionality
 *
 * @author Kamil Dziedzic <arvenil@klecza.pl>
 */
class MockMemcache extends Memcache implements PermanentServiceInterface
{
    
/**
     * @var string[]
     */
    
protected static $data = array();

    
/**
     * Whether the service is available
     * @var boolean
     */
    
protected $available true;

    public function 
__construct()
    {
    }

    
/**
     * @param  string $key
     * @param  mixed  $value
     * @return bool
     */
    
public function add($key$value)
    {
        if (!
$this->available) {
            return 
false;
        }

        if (
false === $this->get($key)) {
            
self::$data[$key] = (string) $value;

            return 
true;
        }

        return 
false;
    }

    
/**
     * @param  string            $key
     * @return array|bool|string
     */
    
public function get($key)
    {
        if (!
$this->available) {
            return 
false;
        }

        if (!isset(
self::$data[$key])) {
            return 
false;
        }

        return (string) 
self::$data[$key];
    }

    
/**
     * @param  string    $key
     * @return bool|void
     */
    
public function delete($key)
    {
        if (!
$this->available) {
            return 
false;
        }

        unset(
self::$data[$key]);

        return 
true;
    }

    
/**
     * @param bool $available
     */
    
public function setAvailable($available)
    {
        
$this->available = (bool) $available;
    }
}