Extract   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B events() 0 52 7
1
<?php
2
3
namespace Spiral\Statistics;
4
5
use Spiral\Statistics\Database\Sources\OccurrenceSource;
6
use Spiral\Statistics\Exceptions\EmptyExtractEventsException;
7
use Spiral\Statistics\Extract\Range;
8
use Spiral\Statistics\Extract\Events;
9
10
class Extract
11
{
12
    /** @var OccurrenceSource */
13
    protected $source;
14
15
    /** @var DatetimeConverter */
16
    protected $converter;
17
18
    /**
19
     * Extract constructor.
20
     *
21
     * @param OccurrenceSource  $source
22
     * @param DatetimeConverter $converter
23
     */
24
    public function __construct(OccurrenceSource $source, DatetimeConverter $converter)
25
    {
26
        $this->source = $source;
27
        $this->converter = $converter;
28
    }
29
30
    /**
31
     * @param \DateTime          $start
32
     * @param \DateTimeInterface $end
33
     * @param string             $rangeInput
34
     * @param array              $eventsInput
35
     * @return Events
36
     */
37
    public function events(
38
        \DateTime $start,
39
        \DateTimeInterface $end,
40
        string $rangeInput,
41
        array $eventsInput
42
    ): Events
43
    {
44
        if (empty($eventsInput)) {
45
            throw new EmptyExtractEventsException();
46
        }
47
48
        $start = $this->converter->immutable($start);
49
        $end = $this->converter->immutable($end);
50
51
        //Swap start and end dates if incorrect
52
        if ($start > $end) {
53
            list($start, $end) = [$end, $start];
54
        }
55
56
        $range = new Range($rangeInput);
57
        $events = new Events($eventsInput);
58
59
        /** @var \DateTime $iteratedDatetime */
60
        $iteratedDatetime = clone $start;
61
62
        //do-while allows to add events of last interval occurrence.
63
        do {
64
            $row = $events->addRow($iteratedDatetime->format($range->getFormat()));
65
66
            $datetime = $this->converter->convert($iteratedDatetime, $rangeInput);
67
68
            foreach ($this->source->findByGroupedInterval(
69
                $range->getField(),
70
                $datetime,
71
                $start,
72
                $end,
73
                $eventsInput
74
            ) as $occurrence) {
75
                foreach ($occurrence->events as $event) {
76
                    if (!in_array($event->name, $eventsInput)) {
77
                        continue;
78
                    }
79
80
                    $row->addEvent($event->name, $event->value);
81
                }
82
            }
83
84
            $iteratedDatetime = $iteratedDatetime->add($range->getInterval());
85
        } while ($iteratedDatetime <= $end);
86
87
        return $events;
88
    }
89
}