BookingClasses::__invoke()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.439
cc 5
eloc 14
nc 8
nop 2
1
<?php
2
3
namespace JhFlexiTime\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
7
/**
8
 * Class BookingClasses
9
 * @package JhFlexiTime\View\Helper
10
 * @author Aydin Hassan <[email protected]>
11
 */
12
class BookingClasses extends AbstractHelper
13
{
14
15
    /**
16
     * Classes for various conditions
17
     *
18
     * @var array
19
     */
20
    protected $classes = [
21
        'noBooking'         => 'no-booking',
22
        'pastNoBooking'     => 'no-booking-past',
23
        'futureNoBooking'   => 'no-booking-future',
24
        'currentDay'        => 'today',
25
    ];
26
27
    /**
28
     * @param \DateTime $date
29
     * @param bool $bookingExists
30
     * @return string
31
     */
32
    public function __invoke(\DateTime $date, $bookingExists)
33
    {
34
        $classes = [];
35
36
        $today = new \DateTime('today');
37
        if ($today->format('d-m-y') == $date->format('d-m-y')) {
38
            $classes[] = $this->getClass('currentDay');
39
        }
40
41
        //if there is no booking for this day then we add a class
42
        //for past or future missing bookings
43
        if (!$bookingExists) {
44
45
            $classes[] = $this->getClass('noBooking');
46
            $diff = $today->diff($date);
47
            //get number of days diff with sign = -15, +10
48
            $days   = (int) $diff->format('%r%a');
49
50
            if ($days > 0) {
51
                //day is in future
52
                $classes[] = $this->getClass('futureNoBooking');
53
            } elseif ($days < 0) {
54
                //day is in past
55
                $classes[] = $this->getClass('pastNoBooking');
56
            }
57
        }
58
59
        return implode(' ', $classes);
60
    }
61
62
    /**
63
     * @param string $type
64
     * @return string
65
     */
66
    public function getClass($type)
67
    {
68
        return $this->classes[$type];
69
    }
70
}
71