Issues (3627)

Segment/Decorator/Date/DateOptionParameters.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Segment\Decorator\Date;
13
14
use Mautic\CoreBundle\Helper\DateTimeHelper;
15
use Mautic\LeadBundle\Segment\ContactSegmentFilterCrate;
16
17
class DateOptionParameters
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $hasTimePart;
23
24
    /**
25
     * @var string
26
     */
27
    private $timeframe;
28
29
    /**
30
     * @var bool
31
     */
32
    private $requiresBetween;
33
34
    /**
35
     * @var bool
36
     */
37
    private $shouldUseLastDayOfRange;
38
39
    /**
40
     * @var DateTimeHelper
41
     */
42
    private $dateTimeHelper;
43
44
    public function __construct(
45
        ContactSegmentFilterCrate $leadSegmentFilterCrate,
46
        array $relativeDateStrings,
47
        TimezoneResolver $timezoneResolver
48
    ) {
49
        $this->hasTimePart             = $leadSegmentFilterCrate->hasTimeParts();
50
        $this->timeframe               = $this->parseTimeFrame($leadSegmentFilterCrate, $relativeDateStrings);
51
        $this->requiresBetween         = in_array($leadSegmentFilterCrate->getOperator(), ['=', '!='], true);
52
        $this->shouldUseLastDayOfRange = in_array($leadSegmentFilterCrate->getOperator(), ['gt', 'lte'], true);
53
54
        $this->setDateTimeHelper($timezoneResolver);
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function hasTimePart()
61
    {
62
        return $this->hasTimePart;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getTimeframe()
69
    {
70
        return $this->timeframe;
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    public function isBetweenRequired()
77
    {
78
        return $this->requiresBetween;
79
    }
80
81
    /**
82
     * This function indicates that we need to modify date to the last date of range.
83
     * "Less than or equal" operator means that we need to include whole week / month / year > last day from range
84
     * "Grater than" needs same logic.
85
     *
86
     * @return bool
87
     */
88
    public function shouldUseLastDayOfRange()
89
    {
90
        return $this->shouldUseLastDayOfRange;
91
    }
92
93
    /**
94
     * @return DateTimeHelper
95
     */
96
    public function getDefaultDate()
97
    {
98
        return $this->dateTimeHelper;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    private function parseTimeFrame(ContactSegmentFilterCrate $leadSegmentFilterCrate, array $relativeDateStrings)
105
    {
106
        $key = array_search($leadSegmentFilterCrate->getFilter(), $relativeDateStrings, true);
107
108
        if (false === $key) {
109
            // Time frame does not match any option from $relativeDateStrings, so return original value
110
            return $leadSegmentFilterCrate->getFilter();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $leadSegmentFilterCrate->getFilter() also could return the type array|boolean which is incompatible with the documented return type string.
Loading history...
111
        }
112
113
        return str_replace('mautic.lead.list.', '', $key);
114
    }
115
116
    private function setDateTimeHelper(TimezoneResolver $timezoneResolver)
117
    {
118
        $this->dateTimeHelper = $timezoneResolver->getDefaultDate($this->hasTimePart());
119
    }
120
}
121