Basic::getEvents()   B
last analyzed

Complexity

Conditions 10
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 7.6666
c 0
b 0
f 0
cc 10
nc 3
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Provider;
13
14
use CalendR\Event\EventInterface;
15
16
/**
17
 * Basic event provider.
18
 * Add and retrieve events like with an array.
19
 *
20
 * @author Yohan Giarelli <[email protected]>
21
 */
22
class Basic implements ProviderInterface, \IteratorAggregate, \Countable
23
{
24
    /**
25
     * @var EventInterface[]
26
     */
27
    protected $events = array();
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getEvents(\DateTime $begin, \DateTime $end, array $options = array())
33
    {
34
        $events = array();
35
        foreach ($this->events as $event) {
36
            if (
37
                ($event->getBegin() >= $begin && $event->getBegin() < $end) ||
38
                ($event->getEnd() > $begin && $event->getEnd() <= $end) ||
39
                ($begin <= $event->getBegin() && $event->getEnd() <= $end) ||
40
                ($event->getBegin() <= $begin && $end <= $event->getEnd())
41
            ) {
42
                $events[] = $event;
43
            }
44
        }
45
46
        return $events;
47
    }
48
49
    /**
50
     * Adds an event to the provider.
51
     *
52
     * @param EventInterface $event
53
     */
54
    public function add(EventInterface $event)
55
    {
56
        $this->events[] = $event;
57
    }
58
59
    /**
60
     * Returns all events.
61
     *
62
     * @return EventInterface[]
63
     */
64
    public function all()
65
    {
66
        return $this->events;
67
    }
68
69
    /**
70
     * Retrieve an external iterator.
71
     *
72
     * @return \Traversable
73
     */
74
    public function getIterator()
75
    {
76
        return new \ArrayIterator($this->events);
77
    }
78
79
    /**
80
     * The return value is cast to an integer.
81
     *
82
     * @return int
83
     */
84
    public function count()
85
    {
86
        return count($this->events);
87
    }
88
}
89