Completed
Push — master ( a017c2...285708 )
by GBProd
02:17
created

NotFactory::validate()   A

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