RunningBalanceCliController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 98
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A reCalcRunningBalanceAction() 0 22 3
A calcPrevMonthBalanceAction() 0 9 1
A setUserStartingBalanceAction() 0 21 2
1
<?php
2
3
namespace JhFlexiTime\Controller;
4
5
use JhFlexiTime\Service\RunningBalanceService;
6
use Zend\Mvc\Controller\AbstractActionController;
7
use Zend\Console\Request as ConsoleRequest;
8
use Doctrine\Common\Persistence\ObjectManager;
9
use JhUser\Repository\UserRepositoryInterface;
10
use JhUser\Repository\RoleRepositoryInterface;
11
use Zend\Console\Adapter\AdapterInterface;
12
use Zend\Console\ColorInterface;
13
14
/**
15
 * Class RoleController
16
 * @package JhUser\Controller
17
 * @author Aydin Hassan <[email protected]>
18
 */
19
class RunningBalanceCliController extends AbstractActionController
20
{
21
    /**
22
     * @var \JhUser\Repository\UserRepositoryInterface
23
     */
24
    protected $userRepository;
25
26
    /**
27
     * @var \JhFlexiTime\Service\RunningBalanceService
28
     */
29
    protected $runningBalanceService;
30
31
    /**
32
     * @param UserRepositoryInterface $userRepository
33
     * @param RunningBalanceService $runningBalanceService
34
     * @param AdapterInterface $console
35
     */
36
    public function __construct(
37
        UserRepositoryInterface $userRepository,
38
        RunningBalanceService $runningBalanceService,
39
        AdapterInterface $console
40
    ) {
41
        $this->userRepository           = $userRepository;
42
        $this->runningBalanceService    = $runningBalanceService;
43
        $this->console                  = $console;
0 ignored issues
show
Bug introduced by
The property console does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
    }
45
46
    /**
47
     * Recaulculate running balance,
48
     * for either a single user or all users
49
     *
50
     * @throws \RuntimeException
51
     */
52
    public function reCalcRunningBalanceAction()
53
    {
54
        $request    = $this->getRequest();
55
        $email      = $request->getParam('userEmail');
56
57
        if ($email) {
58
            $user = $this->userRepository->findOneByEmail($email);
59
60
            if (!$user) {
61
                throw new \RuntimeException(sprintf('User with email: "%s" could not be found', $email));
62
            }
63
64
            $this->console->writeLine("Recalculating Running Balance for $email", ColorInterface::GREEN);
65
            $this->runningBalanceService->reIndexIndividualUserRunningBalance($user);
66
        } else {
67
            $this->console->writeLine("Recalculating Running Balance for all Users", ColorInterface::GREEN);
68
            $this->runningBalanceService->reIndexAllUsersRunningBalance();
69
70
        }
71
72
        $this->console->writeLine("Finished! ", ColorInterface::GREEN);
73
    }
74
75
    /**
76
     * Calculate the balance for the previous month,
77
     * for each user, and add it on to their running balance
78
     */
79
    public function calcPrevMonthBalanceAction()
80
    {
81
        $this->console->writeLine(
82
            "Calculating Running Balance for all Users for previous month",
83
            ColorInterface::GREEN
84
        );
85
        $this->runningBalanceService->indexPreviousMonthBalance();
86
        $this->console->writeLine("Finished! ", ColorInterface::GREEN);
87
    }
88
89
    /**
90
     * Set a Users Initial Balance
91
     * + then recalculate their running balance
92
     *
93
     * @throws \RuntimeException
94
     */
95
    public function setUserStartingBalanceAction()
96
    {
97
        $request    = $this->getRequest();
98
        $balance    = $request->getParam('balance');
99
        $email      = $request->getParam('userEmail');
100
101
        $user = $this->userRepository->findOneByEmail($email);
102
103
        if (!$user) {
104
            throw new \RuntimeException(sprintf('User with email: "%s" could not be found', $email));
105
        }
106
107
        $this->runningBalanceService->setUserStartingBalance($user, $balance);
108
        //recalculate balance
109
        $this->runningBalanceService->reIndexIndividualUserRunningBalance($user);
110
        $this->console->writeLine(
111
            sprintf("Successfully set User '%s' balance to '%s'! ", $email, $balance),
112
            ColorInterface::GREEN
113
        );
114
115
    }
116
}
117