Completed
Branch EDTR/master (6bd139)
by
unknown
35:03 queued 26:41
created

RouteCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getIdentifier() 0 6 2
A getRoutesForCurrentRequest() 0 15 3
A handleRoutesForCurrentRequest() 0 9 2
1
<?php
2
3
namespace EventEspresso\core\services\routing;
4
5
use EventEspresso\core\domain\entities\routing\handlers\RouteInterface;
6
use EventEspresso\core\exceptions\InvalidInterfaceException;
7
use EventEspresso\core\services\collections\Collection;
8
9
/**
10
 * Class RouteCollection
11
 * SplObjectStorage Collection of \EventEspresso\core\domain\entities\routing\handlers\RouteInterface objects
12
 *
13
 * @package EventEspresso\core\services\routing
14
 * @author  Brent Christensen
15
 * @since   4.9.71.p
16
 */
17
class RouteCollection extends Collection
18
{
19
20
    const COLLECTION_NAME = 'routes';
21
22
23
    /**
24
     * RouteMatchSpecificationCollection constructor
25
     *
26
     * @throws InvalidInterfaceException
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct(
31
            'EventEspresso\core\domain\entities\routing\handlers\RouteInterface',
32
            RouteCollection::COLLECTION_NAME
33
        );
34
    }
35
36
37
    /**
38
     * getIdentifier
39
     * Overrides EventEspresso\core\services\collections\Collection::getIdentifier()
40
     * If no $identifier is supplied, then the  fully qualified class name is used
41
     *
42
     * @param        $object
43
     * @param mixed  $identifier
44
     * @return bool
45
     */
46
    public function getIdentifier($object, $identifier = null)
47
    {
48
        return ! empty($identifier)
49
            ? $identifier
50
            : get_class($object);
51
    }
52
53
54
    /**
55
     * finds and returns all Routes that have yet to be handled
56
     *
57
     * @return RouteInterface[]
58
     */
59
    public function getRoutesForCurrentRequest()
60
    {
61
        $routes = [];
62
        $this->rewind();
63
        while ($this->valid()) {
64
            /** @var RouteInterface $route */
65
            $route = $this->current();
66
            if ($route->matchesCurrentRequest()) {
67
                $routes[] = $route;
68
            }
69
            $this->next();
70
        }
71
        $this->rewind();
72
        return $routes;
73
    }
74
75
76
    /**
77
     * calls RouteInterface::handleRequest() on all Routes that
78
     *      - match current request
79
     *      - have yet to be handled
80
     *
81
     * @return void
82
     */
83
    public function handleRoutesForCurrentRequest()
84
    {
85
        $this->rewind();
86
        while ($this->valid()) {
87
            $this->current()->handleRequest();
88
            $this->next();
89
        }
90
        $this->rewind();
91
    }
92
}
93