Completed
Pull Request — master (#170)
by Carsten
03:56
created

AbstractTimeTable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
handleConfiguration() 0 1 ?
A buildSingleTimeTableByGroup() 0 10 3
A calculateEntryKey() 0 5 1
1
<?php
2
/**
3
 * Abstract time table service
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
namespace HDNET\Calendarize\Service\TimeTable;
8
9
use HDNET\Calendarize\Domain\Model\Configuration;
10
use HDNET\Calendarize\Domain\Model\ConfigurationGroup;
11
use HDNET\Calendarize\Service\AbstractService;
12
13
/**
14
 * Abstract time table service
15
 *
16
 * @author Tim Lochmüller
17
 */
18
abstract class AbstractTimeTable extends AbstractService
19
{
20
21
    /**
22
     * Seconds of 23:59:59 that mark the day end
23
     */
24
    const DAY_END = 86399;
25
26
    /**
27
     * Time table service
28
     *
29
     * @var \HDNET\Calendarize\Service\TimeTableService
30
     * @inject
31
     */
32
    protected $timeTableService;
33
34
    /**
35
     * Modify the given times via the configuration
36
     *
37
     * @param array         $times
38
     * @param Configuration $configuration
39
     *
40
     * @return void
41
     */
42
    abstract public function handleConfiguration(array &$times, Configuration $configuration);
43
44
    /**
45
     * Build a single time table by group
46
     *
47
     * @param ConfigurationGroup $group
48
     *
49
     * @return array
50
     */
51
    protected function buildSingleTimeTableByGroup(ConfigurationGroup $group)
52
    {
53
        $ids = [];
54
        foreach ($group->getConfigurations() as $configuration) {
55
            if ($configuration instanceof Configuration) {
56
                $ids[] = $configuration->getUid();
57
            }
58
        }
59
        return $this->timeTableService->getTimeTablesByConfigurationIds($ids);
60
    }
61
62
    /**
63
     * Calculate a hash for the key of the given entry.
64
     * This prevent double entries in the index.
65
     *
66
     * @param array $entry
67
     *
68
     * @return string
69
     */
70
    protected function calculateEntryKey(array $entry)
71
    {
72
        // crc32 may be faster but have more collision-potential
73
        return \hash('md5', \json_encode($entry));
74
    }
75
}
76