RedisCounter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A count() 0 11 3
A getCounts() 0 4 1
1
<?php
2
3
namespace MeadSteve\DiceApi\Counters;
4
5
use MeadSteve\DiceApi\Dice;
6
use Predis\Client;
7
use Predis\Connection\ConnectionException;
8
9
class RedisCounter implements DiceCounter
10
{
11
    /**
12
     * @var Client
13
     */
14
    private $redisClient;
15
16
    public function __construct(Client $redisClient)
17
    {
18
        $this->redisClient = $redisClient;
19
    }
20
21
    /**
22
     * @param Dice[] $diceCollection
23
     * @return bool
24
     */
25
    public function count(array $diceCollection)
26
    {
27
        try {
28
            foreach ($diceCollection as $dice) {
29
                $this->redisClient->hincrby("dice-count", $dice->name(), 1);
30
            }
31
        } catch (ConnectionException $connectError) {
32
            return false;
33
        }
34
        return true;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getCounts()
41
    {
42
        return $this->redisClient->hgetall("dice-count");
43
    }
44
}
45