Completed
Branch BUG/reg-status-change-recursio... (2db0c9)
by
unknown
20:03 queued 10:32
created

RouteMatchSpecificationManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A initialize() 0 4 1
A populateSpecificationCollection() 0 30 1
A routeMatchesCurrentRequest() 0 9 2
A findRouteMatchSpecificationsMatchingCurrentRequest() 0 11 3
1
<?php
2
3
namespace EventEspresso\core\services\route_match;
4
5
use EventEspresso\core\domain\entities\route_match\RouteMatchSpecificationInterface;
6
use EventEspresso\core\exceptions\InvalidClassException;
7
use EventEspresso\core\services\collections\CollectionDetails;
8
use EventEspresso\core\services\collections\CollectionDetailsException;
9
use EventEspresso\core\services\collections\CollectionInterface;
10
use EventEspresso\core\services\collections\CollectionLoader;
11
use EventEspresso\core\services\collections\CollectionLoaderException;
12
13
/**
14
 * Class RouteMatchSpecificationManager
15
 * Manages setup of the RouteMatchSpecificationsCollection
16
 * and compares specifications in it via the currentRequestMatches() method.
17
 * Folders containing RouteMatchSpecification classes can be added to the collection using the
18
 * FHEE__EventEspresso_core_services_route_match_RouteMatchSpecificationManager__populateSpecificationCollection__collection_FQCNs
19
 * filter prior to the AHEE__EE_System__initialize hookpoint
20
 *
21
 *
22
 * @package EventEspresso\core\services\route_match
23
 * @author  Brent Christensen
24
 * @since   $VID:$
25
 */
26
class RouteMatchSpecificationManager
27
{
28
    /**
29
     * @var CollectionInterface[]|RouteMatchSpecificationInterface[] $specifications
30
     */
31
    private $specifications;
32
33
    /**
34
     * @var RouteMatchSpecificationFactory $specifications_factory
35
     */
36
    private $specifications_factory;
37
38
39
    /**
40
     * RouteMatchSpecificationManager constructor.
41
     *
42
     * @param RouteMatchSpecificationCollection $specifications
43
     * @param RouteMatchSpecificationFactory    $specifications_factory
44
     */
45
    public function __construct(
46
        RouteMatchSpecificationCollection $specifications,
47
        RouteMatchSpecificationFactory $specifications_factory
48
    ) {
49
        $this->specifications = $specifications;
50
        $this->specifications_factory = $specifications_factory;
51
        add_action('AHEE__EE_System__loadRouteMatchSpecifications', array($this, 'initialize'));
52
    }
53
54
55
    /**
56
     * Perform any early setup required for block editors to functions
57
     *
58
     * @return void
59
     * @throws CollectionLoaderException
60
     * @throws CollectionDetailsException
61
     */
62
    public function initialize()
63
    {
64
        $this->populateSpecificationCollection();
65
    }
66
67
68
    /**
69
     * @return CollectionInterface|RouteMatchSpecificationInterface[]
70
     * @throws CollectionLoaderException
71
     * @throws CollectionDetailsException
72
     * @since $VID:$
73
     */
74
    private function populateSpecificationCollection()
75
    {
76
        $loader = new CollectionLoader(
77
            new CollectionDetails(
78
                // collection name
79
                RouteMatchSpecificationCollection::COLLECTION_NAME,
80
                // collection interface
81
                'EventEspresso\core\domain\entities\route_match\RouteMatchSpecificationInterface',
82
                // FQCNs for classes to add (all classes within each namespace will be loaded)
83
                apply_filters(
84
                    'FHEE__EventEspresso_core_services_route_match_RouteMatchSpecificationManager__populateSpecificationCollection__collection_FQCNs',
85
                    array(
86
                        'EventEspresso\core\domain\entities\route_match\specifications\admin',
87
                        'EventEspresso\core\domain\entities\route_match\specifications\frontend',
88
                    )
89
                ),
90
                // filepaths to classes to add
91
                array(),
92
                // file mask to use if parsing folder for files to add
93
                '',
94
                // what to use as identifier for collection entities
95
                // using CLASS NAME prevents duplicates (works like a singleton)
96
                CollectionDetails::ID_CLASS_NAME
97
            ),
98
            $this->specifications,
99
            null,
100
            $this->specifications_factory
101
        );
102
        return $loader->getCollection();
103
    }
104
105
106
    /**
107
     * Given the FQCN for a RouteMatchSpecification, will return true if the current request matches
108
     *
109
     * @param string $routeMatchSpecificationFqcn fully qualified class name
110
     * @return bool
111
     * @throws InvalidClassException
112
     * @since $VID:$
113
     */
114
    public function routeMatchesCurrentRequest($routeMatchSpecificationFqcn)
115
    {
116
        /** @var RouteMatchSpecificationInterface $specification */
117
        $specification = $this->specifications->get($routeMatchSpecificationFqcn);
118
        if (! $specification instanceof $routeMatchSpecificationFqcn) {
119
            throw new InvalidClassException($routeMatchSpecificationFqcn);
120
        }
121
        return $specification->isMatchingRoute();
122
    }
123
124
125
    /**
126
     * Handy method for development that returns
127
     * a list of existing RouteMatchSpecification classes
128
     * matching the current request that can be used to identify it.
129
     * If no matches are returned (or nothing acceptable)
130
     * then create a new one that better matches your requirements.
131
     *
132
     * @return array
133
     * @since $VID:$
134
     */
135
    public function findRouteMatchSpecificationsMatchingCurrentRequest()
136
    {
137
        $matches = array();
138
        foreach ($this->specifications as $specification) {
139
            /** @var RouteMatchSpecificationInterface $specification */
140
            if ($specification->isMatchingRoute()) {
141
                $matches[] = get_class($specification);
142
            }
143
        }
144
        return $matches;
145
    }
146
}
147