Completed
Pull Request — master (#8)
by steef
03:12
created

Parser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 34.62%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 81
ccs 9
cts 26
cp 0.3462
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C getAllValidDates() 0 23 7
A findSpecialDateByDateTime() 0 19 3
A findSpecialDateByMonthNumber() 0 16 3
1
<?php
2
3
namespace Stefanius\SpecialDates\DateParser;
4
5
use Stefanius\SpecialDates\SDK\SpecialDateInterface;
6
7
class Parser extends AbstractParser
8
{
9
    /**
10
     * @param $year
11
     * @param bool $singleDay
12
     * @param bool $multiDays
13
     *
14
     * @return array
15
     */
16
    public function getAllValidDates($year, $singleDay = true, $multiDays = true)
17
    {
18
        $items = $this->getAllDates($year);
19
20
        /**
21
         * @var $value SpecialDateInterface
22
         */
23
        foreach ($items as $key => $value) {
24
            if ($value->isValid() === false) {
25
                unset($items[$key]);
26
            }
27
28
            if (!$singleDay && $value->getTotalLength() === 1) {
29
                unset($items[$key]);
30
            }
31
32
            if (!$multiDays && $value->getTotalLength() > 1) {
33
                unset($items[$key]);
34
            }
35
        }
36
37
        return $items;
38
    }
39
40
    /**
41
     * @param \DateTime $date
42
     *
43
     * @return array
44
     */
45 1
    public function findSpecialDateByDateTime(\DateTime $date)
46
    {
47 1
        $formattedDate = $date->format('Y') . '-' . $date->format('m') . $date->format('d');
48 1
        $items = $this->getAllDates($date->format('Y'));
49 1
        $found = [];
50
51
        /**
52
         * @var $item SpecialDateInterface
53
         */
54 1
        foreach ($items as $key => $item) {
55 1
            $formattedStartDate = $item->getStartDate()->format('Y') . '-' . $item->getStartDate()->format('m') . $item->getStartDate()->format('d');
56
57 1
            if ($formattedDate === $formattedStartDate) {
58 1
                $found[$key] = $item;
59
            }
60
        }
61
62 1
        return $found;
63
    }
64
65
    /**
66
     * @param $year
67
     * @param $month
68
     *
69
     * @return array
70
     */
71
    public function findSpecialDateByMonthNumber($year, $month)
72
    {
73
        $items = $this->getAllDates($year);
74
        $found = [];
75
76
        /**
77
         * @var $item SpecialDateInterface
78
         */
79
        foreach ($items as $key => $item) {
80
            if ((int)$month === (int)$item->getStartDate()->format('m')) {
81
                $found[$key] = $item;
82
            }
83
        }
84
85
        return $found;
86
    }
87
}
88