NotFactory::validate()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 3
nc 2
nop 1
crap 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