CappedCreditListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace JhFlexiTime\Listener;
4
5
use JhFlexiTime\DateTime\DateTime;
6
use JhFlexiTime\Entity\RunningBalance;
7
use JhFlexiTime\Options\ModuleOptions;
8
use JhFlexiTime\Service\CappedCreditService;
9
use JhFlexiTime\Service\RunningBalanceService;
10
use Zend\EventManager\EventManagerInterface;
11
use Zend\EventManager\AbstractListenerAggregate;
12
use Zend\EventManager\Event;
13
14
/**
15
 * Class CappedCreditListener
16
 * @package JhFlexiTime\Listener
17
 * @author Aydin Hassan <[email protected]>
18
 */
19
class CappedCreditListener extends AbstractListenerAggregate
20
{
21
    /**
22
     * @var CappedCreditService
23
     */
24
    protected $cappedCreditService;
25
26
    /**
27
     * @var ModuleOptions
28
     */
29
    protected $options;
30
31
    /**
32
     * @param CappedCreditService $cappedCreditService
33
     * @param ModuleOptions $options
34
     */
35
    public function __construct(CappedCreditService $cappedCreditService, ModuleOptions $options)
36
    {
37
        $this->cappedCreditService  = $cappedCreditService;
38
        $this->options              = $options;
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function attach(EventManagerInterface $events)
45
    {
46
        if (!$this->options->creditCapEnabled()) {
47
            return;
48
        }
49
50
        $sharedEvents = $events->getSharedManager();
51
52
        $this->listeners[] = $sharedEvents->attach(
53
            'JhFlexiTime\Service\RunningBalanceService',
54
            'reIndexUserRunningBalance.pre',
55
            [$this, 'clearCappedCreditRecords'],
56
            100
57
        );
58
59
        $this->listeners[] = $sharedEvents->attach(
60
            'JhFlexiTime\Service\RunningBalanceService',
61
            'addMonthBalance.post',
62
            [$this, 'applyCreditCarryLimit'],
63
            100
64
        );
65
66
    }
67
68
    /**
69
     * @param Event $e
70
     */
71
    public function clearCappedCreditRecords(Event $e)
72
    {
73
        /** @var RunningBalance $runningBalance */
74
        $runningBalance = $e->getParam('runningBalance');
75
        $this->cappedCreditService->clearCappedCreditEntries($runningBalance->getUser());
76
    }
77
78
    /**
79
     * @param Event $e
80
     */
81
    public function applyCreditCarryLimit(Event $e)
82
    {
83
84
        /** @var RunningBalance $runningBalance */
85
        $runningBalance = $e->getParam('runningBalance');
86
        $month          = $e->getParam('month');
87
88
        /**
89
         * If running balance is over allowed credit limit, change it to the limit
90
         * Store the difference so it may be used for other things. Eg trade for overtime etc.
91
         */
92
        $creditLimit = $this->options->getCreditCapForDate($month);
93
        if (null !== $creditLimit && $runningBalance->getBalance() > $creditLimit) {
94
            $overage        = $runningBalance->getBalance() - $creditLimit;
95
            $runningBalance->setBalance($creditLimit);
96
97
            $this->cappedCreditService->create($runningBalance->getUser(), $overage, $month);
98
        }
99
    }
100
}
101