DateRangesTools::cleanParams()   C
last analyzed

Complexity

Conditions 13
Paths 20

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 13
eloc 20
c 1
b 1
f 0
nc 20
nop 1
dl 0
loc 32
rs 6.6166

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
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2024 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace App\Utility;
17
18
use Cake\Utility\Hash;
19
20
/**
21
 * Date Ranges tools
22
 */
23
class DateRangesTools
24
{
25
    /**
26
     * Prepare date ranges.
27
     *
28
     * @param array $inputDateRanges Date ranges to format.
29
     * @return array
30
     */
31
    public static function prepare(array $inputDateRanges): array
32
    {
33
        $dateRanges = $inputDateRanges;
34
        $dateRanges = array_filter(
35
            (array)$dateRanges,
36
            function ($item) {
37
                $sd = (string)Hash::get($item, 'start_date');
38
                $ed = (string)Hash::get($item, 'end_date');
39
40
                return !empty($sd) || !empty($ed);
41
            }
42
        );
43
        $dateRanges = array_map(
44
            function ($item) {
45
                $ed = (string)Hash::get($item, 'end_date');
46
                if (empty($ed)) {
47
                    return $item;
48
                }
49
                $item['end_date'] = str_replace(':59:00.000', ':59:59.000', $ed);
50
51
                return $item;
52
            },
53
            $dateRanges
54
        );
55
        foreach ($dateRanges as &$item) {
56
            if (empty(Hash::get($item, 'params'))) {
57
                continue;
58
            }
59
            $params = Hash::get($item, 'params');
60
            $params = is_string($params) ? json_decode($params, true) : (array)$params;
61
            $item['params'] = self::parseParams($params, self::isOneDayRange((array)$item));
62
        }
63
        $dateRanges = array_values($dateRanges);
64
65
        return $dateRanges;
66
    }
67
68
    /**
69
     * Check if date range is one day only.
70
     *
71
     * @param array $dateRange Date range.
72
     * @return bool
73
     */
74
    public static function isOneDayRange(array $dateRange): bool
75
    {
76
        $sd = (string)Hash::get($dateRange, 'start_date');
77
        $ed = (string)Hash::get($dateRange, 'end_date');
78
79
        return empty($ed) || (substr($sd, 0, 10) === substr($ed, 0, 10));
80
    }
81
82
    /**
83
     * Parse params.
84
     *
85
     * @param array $params Params to parse.
86
     * @param bool $oneDayRange Is one day range.
87
     * @return array|null
88
     */
89
    public static function parseParams(array $params, bool $oneDayRange): ?array
90
    {
91
        $params = self::cleanParams($params);
92
        if (empty($params)) {
93
            return null;
94
        }
95
        // one day range
96
        if ($oneDayRange) {
97
            $allDay = Hash::get($params, 'all_day') === true;
98
99
            return $allDay ? ['all_day' => true, 'every_day' => true] : null;
100
        }
101
        // multi days range
102
103
        return $params === ['every_day' => true] ? null : $params;
104
    }
105
106
    /**
107
     * Clean params, remove all_day false, every_day false, weekdays empty.
108
     *
109
     * @param array $params Params to clean.
110
     * @return array|null
111
     */
112
    public static function cleanParams(array $params): ?array
113
    {
114
        $data = [];
115
        foreach ($params as $key => $value) {
116
            if ($key === 'all_day' && $value === true) {
117
                $data[$key] = true;
118
119
                continue;
120
            }
121
            if ($key === 'every_day' && $value === true) {
122
                $data[$key] = true;
123
124
                continue;
125
            }
126
            if ($key === 'weekdays' && !empty($value)) {
127
                $checked = array_filter($value, function ($v) {
128
                    return $v === true;
129
                });
130
                $count = count($checked);
131
                if ($count > 0) {
132
                    $data['weekdays'] = $value;
133
                }
134
                if ($count === 7 || $count === 0) {
135
                    $data['every_day'] = true;
136
                    unset($data[$key]);
137
                } elseif ($params['every_day'] === true) {
138
                    unset($data[$key]);
139
                }
140
            }
141
        }
142
143
        return empty($data) ? null : $data;
144
    }
145
146
    /**
147
     * Convert date ranges to string.
148
     *
149
     * @param array $dateRanges Date ranges to convert.
150
     * @return string
151
     */
152
    public static function toString(array $dateRanges): string
153
    {
154
        $drs = [];
155
        foreach ($dateRanges as $dateRange) {
156
            $startDate = (string)Hash::get($dateRange, 'start_date');
157
            $endDate = (string)Hash::get($dateRange, 'end_date');
158
            $params = json_encode((array)Hash::get($dateRange, 'params', []));
159
160
            $drs[] = sprintf(
161
                '%s-%s-%s',
162
                $startDate,
163
                $endDate,
164
                $params
165
            );
166
        }
167
168
        return implode(',', $drs);
169
    }
170
}
171