NotFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 16 2
A validate() 0 6 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace GBProd\AlgoliaSpecification\QueryFactory;
6
7
use GBProd\AlgoliaSpecification\Registry;
8
use GBProd\Specification\AndX;
9
use GBProd\Specification\Not;
10
use GBProd\Specification\OrX;
11
use GBProd\Specification\Specification;
12
13
/**
14
 * Factory for Not specification
15
 *
16
 * @author gbprod <[email protected]>
17
 */
18
class NotFactory implements Factory
19
{
20
    /**
21
     * @var Registry
22
     */
23
    private $registry;
24
25
    /**
26
     * @param Registry $registry
27
     */
28 7
    public function __construct(Registry $registry)
29
    {
30 7
        $this->registry = $registry;
31 7
    }
32
33
    /**
34
     * {inheritdoc}
35
     */
36 3
    public function create(Specification $spec): string
37
    {
38 3
        if (!$spec instanceof Not) {
39 1
            throw new \InvalidArgumentException();
40
        }
41
42 2
        $wrappedSpec = $spec->getWrappedSpecification();
43 2
        $this->validate($wrappedSpec);
44
45 1
        $firstPartFactory = $this->registry->getFactory($wrappedSpec);
46
47 1
        return sprintf(
48 1
            'NOT %s',
49 1
            $firstPartFactory->create($wrappedSpec)
50
        );
51
    }
52
53
    /**
54
     * Algolia does not allows to negate a group
55
     * @see https://www.algolia.com/doc/api-client/php/parameters#filters
56
     *
57
     * @throws \InvalidArgumentException
58
     */
59 2
    private function validate(Specification $spec)
60
    {
61 2
        if ($spec instanceof Not || $spec instanceof AndX || $spec instanceof OrX) {
62 1
            throw new \InvalidArgumentException('Algolia does not allows to negate a group');
63
        }
64 1
    }
65
}
66