Completed
Push — master ( 988d8e...10d093 )
by Kévin
03:02
created

ComposedSpecification   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 40
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
namespace RulerZ\Spec;
4
5
/**
6
 * Used to compose specification in a single class.
7
 *
8
 * Sample usage:
9
 *
10
 * ```
11
 * class AccessibleBy extends \RulerZ\Spec\SpecificationSpecification
12
 * {
13
 *     private $user;
14
 *
15
 *     public function __construct(Entity\User $user)
16
 *     {
17
 *         $this->user = $user;
18
 *     }
19
 *
20
 *     protected function getSpecification()
21
 *     {
22
 *         return (new IsOwner($this->user))->orX(new IsMember($this->user));
23
 *     }
24
 * }
25
 * ```
26
 */
27
abstract class ComposedSpecification extends AbstractSpecification
28
{
29
    private $specification;
30
31
    /**
32
     * The composed specification.
33
     *
34
     * @return Specification
35
     */
36
    abstract protected function getSpecification();
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function getRule()
42
    {
43
        $spec = $this->initializeSpecification();
44
45
        return $spec->getRule();
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function getParameters()
52
    {
53
        $spec = $this->initializeSpecification();
54
55
        return $spec->getParameters();
56
    }
57
58
    private function initializeSpecification()
59
    {
60
        if ($this->specification === null) {
61
            $this->specification = $this->getSpecification();
62
        }
63
64
        return $this->specification;
65
    }
66
}
67