ComposedSpecification   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
getSpecification() 0 1 ?
A getRule() 0 6 1
A getParameters() 0 6 1
A initializeSpecification() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ\Spec;
6
7
/**
8
 * Used to compose specification in a single class.
9
 *
10
 * Sample usage:
11
 *
12
 * ```
13
 * class AccessibleBy extends \RulerZ\Spec\ComposedSpecification
14
 * {
15
 *     private $user;
16
 *
17
 *     public function __construct(Entity\User $user)
18
 *     {
19
 *         $this->user = $user;
20
 *     }
21
 *
22
 *     protected function getSpecification()
23
 *     {
24
 *         return (new IsOwner($this->user))->orX(new IsMember($this->user));
25
 *     }
26
 * }
27
 * ```
28
 */
29
abstract class ComposedSpecification extends AbstractSpecification
30
{
31
    private $specification;
32
33
    /**
34
     * The composed specification.
35
     *
36
     * @return Specification
37
     */
38
    abstract protected function getSpecification();
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getRule(): string
44
    {
45
        $spec = $this->initializeSpecification();
46
47
        return $spec->getRule();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getParameters(): array
54
    {
55
        $spec = $this->initializeSpecification();
56
57
        return $spec->getParameters();
58
    }
59
60
    private function initializeSpecification()
61
    {
62
        if ($this->specification === null) {
63
            $this->specification = $this->getSpecification();
64
        }
65
66
        return $this->specification;
67
    }
68
}
69