Manager::find()   B
last analyzed

Complexity

Conditions 9
Paths 13

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.0555
c 0
b 0
f 0
cc 9
nc 13
nop 2
1
<?php
2
3
/*
4
 * This file is part of CalendR, a Fréquence web project.
5
 *
6
 * (c) 2012 Fréquence web
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CalendR\Event;
13
14
use CalendR\Event\Exception\NoProviderFound;
15
use CalendR\Period\PeriodInterface;
16
use CalendR\Event\Provider\ProviderInterface;
17
18
/**
19
 * Manage events and providers.
20
 *
21
 * @author Yohan Giarelli <[email protected]>
22
 */
23
class Manager
24
{
25
    /**
26
     * @var ProviderInterface[]
27
     */
28
    protected $providers = array();
29
30
    /**
31
     * The callable used to instantiate the event collection.
32
     *
33
     * @var callable
34
     */
35
    protected $collectionInstantiator;
36
37
    /**
38
     * @param array $providers
39
     * @param null  $instantiator
40
     */
41
    public function __construct(array $providers = array(), $instantiator = null)
42
    {
43
        $this->collectionInstantiator = $instantiator;
44
        if (null === $instantiator) {
45
            $this->collectionInstantiator = function () {
46
                return new Collection\Basic();
47
            };
48
        }
49
50
        foreach ($providers as $name => $provider) {
51
            $this->addProvider($name, $provider);
52
        }
53
    }
54
55
    /**
56
     * find events that matches the given period (during or over).
57
     *
58
     * @param \CalendR\Period\PeriodInterface $period
59
     * @param array                           $options
60
     *
61
     * @return array|EventInterface
62
     *
63
     * @throws NoProviderFound
64
     */
65
    public function find(PeriodInterface $period, array $options = array())
0 ignored issues
show
Complexity introduced by
This operation has 200 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
66
    {
67
        if (0 === count($this->providers)) {
68
            throw new NoProviderFound();
69
        }
70
71
        // Check if there's a provider option provided, used to filter the used providers
72
        $providers = isset($options['providers']) ? $options['providers'] : array();
73
        if (!is_array($providers)) {
74
            $providers = array($providers);
75
        }
76
77
        // Instantiate an event collection
78
        $collection = call_user_func($this->collectionInstantiator);
79
        foreach ($this->providers as $name => $provider) {
80
            if (count($providers) > 0 && !in_array($name, $providers)) {
81
                continue;
82
            }
83
84
            // Add matching events to the collection
85
            foreach ($provider->getEvents($period->getBegin(), $period->getEnd(), $options) as $event) {
86
                if ($period->containsEvent($event)) {
87
                    $collection->add($event);
88
                }
89
            }
90
        }
91
92
        return $collection;
93
    }
94
95
    /**
96
     * Adds a provider to the provider stack.
97
     *
98
     * @param $name
99
     * @param ProviderInterface $provider
100
     */
101
    public function addProvider($name, ProviderInterface $provider)
102
    {
103
        $this->providers[$name] = $provider;
104
    }
105
106
    /**
107
     * Sets the callable used to instantiate the event collection.
108
     *
109
     * @param callable $collectionInstantiator
110
     */
111
    public function setCollectionInstantiator($collectionInstantiator)
112
    {
113
        $this->collectionInstantiator = $collectionInstantiator;
114
    }
115
116
    /**
117
     * @return \CalendR\Event\Provider\ProviderInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be ProviderInterface[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
118
     */
119
    public function getProviders()
120
    {
121
        return $this->providers;
122
    }
123
}
124