Client   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 3 1
A getRepository() 0 3 1
A __construct() 0 4 1
A create() 0 15 6
A validateParameters() 0 13 3
A setDriver() 0 14 2
A pushElement() 0 8 1
A setRepository() 0 7 1
1
<?php
2
/**
3
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 */
10
11
namespace InMemoryList\Application;
12
13
use InMemoryList\Application\Exceptions\MalformedParametersException;
14
use InMemoryList\Application\Exceptions\NotSupportedDriverException;
15
use InMemoryList\Domain\Model\Contracts\ListRepositoryInterface;
16
use InMemoryList\Domain\Model\ListElement;
17
use InMemoryList\Domain\Model\ListElementUuid;
18
use InMemoryList\Infrastructure\Domain\Model\ListCollectionFactory;
19
20
class Client
21
{
22
    /**
23
     * @var string
24
     */
25
    private $driver;
26
27
    /**
28
     * @var ListRepositoryInterface
29
     */
30
    private $repository;
31
32
    /**
33
     * Client constructor.
34
     *
35
     * @param string $driver
36
     * @param array  $parameters
37
     */
38
    public function __construct($driver = 'redis', array $parameters = [], $createSchema = false)
0 ignored issues
show
Unused Code introduced by
The parameter $createSchema is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function __construct($driver = 'redis', array $parameters = [], /** @scrutinizer ignore-unused */ $createSchema = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        $this->setDriver($driver);
41
        $this->setRepository($driver, $parameters, $createSchema = false);
0 ignored issues
show
Unused Code introduced by
The call to InMemoryList\Application\Client::setRepository() has too many arguments starting with $createSchema = false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->/** @scrutinizer ignore-call */ 
42
               setRepository($driver, $parameters, $createSchema = false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
    }
43
44
    /**
45
     * @param $driver
46
     *
47
     * @throws NotSupportedDriverException
48
     */
49
    private function setDriver($driver)
50
    {
51
        $allowedDrivers = [
52
            'apcu',
53
            'memcached',
54
            'pdo',
55
            'redis',
56
        ];
57
58
        if (!in_array($driver, $allowedDrivers)) {
59
            throw new NotSupportedDriverException($driver.' is not a supported driver.');
60
        }
61
62
        $this->driver = $driver;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getDriver()
69
    {
70
        return $this->driver;
71
    }
72
73
    /**
74
     * @param $driver
75
     * @param array $config
76
     */
77
    private function setRepository($driver, array $config = [])
78
    {
79
        $repository = 'InMemoryList\Infrastructure\Persistance\\'.ucfirst($driver).'Repository';
80
        $driver = 'InMemoryList\Infrastructure\Drivers\\'.ucfirst($driver).'Driver';
81
        $instance = (new $driver($config))->getInstance();
82
83
        $this->repository = new $repository($instance);
84
    }
85
86
    /**
87
     * @return ListRepositoryInterface
88
     */
89
    public function getRepository()
90
    {
91
        return $this->repository;
92
    }
93
94
    /**
95
     * @param array $elements
96
     * @param array $parameters
97
     *
98
     * @return mixed|string
99
     *
100
     * @throws MalformedParametersException
101
     */
102
    public function create(array $elements, array $parameters = [])
103
    {
104
        $this->validateParameters($parameters);
105
        $factory = new ListCollectionFactory();
106
        $list = $factory->create(
107
            $elements,
108
            (isset($parameters['headers'])) ? $parameters['headers'] : [],
109
            (isset($parameters['uuid'])) ? $parameters['uuid'] : null,
110
            (isset($parameters['element-uuid'])) ? $parameters['element-uuid'] : null
111
        );
112
113
        return $this->repository->create(
114
            $list,
115
            (isset($parameters['ttl'])) ? $parameters['ttl'] : null,
116
            (isset($parameters['chunk-size'])) ? $parameters['chunk-size'] : null
117
        );
118
    }
119
120
    /**
121
     * @param $parameters
122
     *
123
     * @throws MalformedParametersException
124
     */
125
    private function validateParameters($parameters)
126
    {
127
        $allowedParameters = [
128
            'chunk-size',
129
            'element-uuid',
130
            'headers',
131
            'ttl',
132
            'uuid',
133
        ];
134
135
        foreach (array_keys($parameters) as $key) {
136
            if (!in_array($key, $allowedParameters)) {
137
                throw new MalformedParametersException();
138
            }
139
        }
140
    }
141
142
    /**
143
     * @param $listUuid
144
     * @param $elementUuid
145
     * @param array $data
146
     *
147
     * @return mixed
148
     */
149
    public function pushElement($listUuid, $elementUuid, array $data = [])
150
    {
151
        $newElement = new ListElement(
152
            new ListElementUuid($elementUuid),
153
            $data
154
        );
155
156
        return $this->repository->pushElement($listUuid, $newElement);
157
    }
158
}
159