Installer::createRunningBalanceRow()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.439
cc 6
eloc 21
nc 10
nop 2
1
<?php
2
3
namespace JhFlexiTime\Install;
4
5
use Doctrine\DBAL\DBALException;
6
use JhFlexiTime\Entity\RunningBalance;
7
use JhFlexiTime\Entity\UserSettings;
8
use JhFlexiTime\Repository\UserSettingsRepositoryInterface;
9
use JhFlexiTime\Repository\BalanceRepositoryInterface;
10
use JhUser\Repository\UserRepositoryInterface;
11
use Zend\Console\Adapter\AdapterInterface;
12
use Zend\Console\ColorInterface as Color;
13
use Doctrine\Common\Persistence\ObjectManager;
14
use JhInstaller\Install\Exception as InstallException;
15
use JhInstaller\Install\InstallerInterface;
16
use ZfcUser\Entity\UserInterface;
17
use JhFlexiTime\DateTime\DateTime;
18
19
/**
20
 * Class Installer
21
 * @package JhFlexiTime\Install
22
 * @author Aydin Hassan <[email protected]>
23
 */
24
class Installer implements InstallerInterface
25
{
26
    /**
27
     * @var UserRepositoryInterface
28
     */
29
    protected $userRepository;
30
31
    /**
32
     * @var UserSettingsRepositoryInterface
33
     */
34
    protected $userSettingsRepository;
35
36
    /**
37
     * @var BalanceRepositoryInterface
38
     */
39
    protected $balanceRepository;
40
41
    /**
42
     * @var ObjectManager
43
     */
44
    protected $objectManager;
45
46
    /**
47
     * @var array
48
     */
49
    protected $errors = [];
50
51
    /**
52
     * @param UserRepositoryInterface $userRepository
53
     * @param UserSettingsRepositoryInterface $userSettingsRepository
54
     * @param BalanceRepositoryInterface $balanceRepository
55
     * @param ObjectManager $objectManager
56
     */
57
    public function __construct(
58
        UserRepositoryInterface $userRepository,
59
        UserSettingsRepositoryInterface $userSettingsRepository,
60
        BalanceRepositoryInterface $balanceRepository,
61
        ObjectManager $objectManager
62
    ) {
63
        $this->userRepository           = $userRepository;
64
        $this->userSettingsRepository   = $userSettingsRepository;
65
        $this->balanceRepository        = $balanceRepository;
66
        $this->objectManager            = $objectManager;
67
    }
68
69
    /**
70
     * @param AdapterInterface $console
71
     * @throws \JhInstaller\Install\Exception
72
     */
73
    public function install(AdapterInterface $console)
74
    {
75
76
        foreach ($this->userRepository->findAll() as $user) {
77
            $this->createSettingsRow($user, $console);
78
            $this->createRunningBalanceRow($user, $console);
79
        }
80
81
        $this->objectManager->flush();
82
83
    }
84
85
    /**
86
     * @param UserInterface $user
87
     * @param AdapterInterface $console
88
     * @throws \JhInstaller\Install\Exception
89
     */
90
    public function createSettingsRow(UserInterface $user, AdapterInterface $console = null)
91
    {
92
        if ($console) {
93
            $console->writeLine(
94
                sprintf("Checking if user '%s' has a user_flex_settings row..", $user->getEmail()),
95
                Color::YELLOW
96
            );
97
        }
98
99
        try {
100
            $userSettings = $this->userSettingsRepository->findOneByUser($user);
101
        } catch (DBALException $e) {
102
            $this->errors[] = sprintf(
103
                "The Database table has not been created. Be sure to run the schema tool. Message: %s",
104
                $e->getMessage()
105
            );
106
            throw new InstallException();
107
        }
108
109
        if (!$userSettings) {
110
            if ($console) {
111
                $console->writeLine("Settings row does not exist. Creating... ", Color::YELLOW);
112
            }
113
            $userSettings = new UserSettings();
114
            $userSettings
115
                ->setUser($user)
116
                ->setDefaultStartTime(new DateTime("09:00"))
117
                ->setDefaultEndTime(new DateTime("17:00"))
118
                ->setFlexStartDate(new DateTime);
119
120
            $this->objectManager->persist($userSettings);
121
122
        } else {
123
            if ($console) {
124
                $console->writeLine("Settings row exists. Skipping", Color::YELLOW);
125
            }
126
        }
127
    }
128
129
    /**
130
     * @param UserInterface $user
131
     * @param AdapterInterface $console
132
     * @throws \JhInstaller\Install\Exception
133
     */
134
    public function createRunningBalanceRow(UserInterface $user, AdapterInterface $console = null)
135
    {
136
137
        if ($console) {
138
            $console->writeLine(
139
                sprintf("Checking if user '%s' has a running_balance row..", $user->getEmail()),
140
                Color::YELLOW
141
            );
142
        }
143
144
        try {
145
            $runningBalance = $this->balanceRepository->findOneByUser($user);
146
        } catch (DBALException $e) {
147
            $this->errors[] = sprintf(
148
                "The Database table has not been created. Be sure to run the schema tool. Message: %s",
149
                $e->getMessage()
150
            );
151
            throw new InstallException();
152
        }
153
154
        if (!$runningBalance) {
155
156
            if ($console) {
157
                $console->writeLine("Running Balance row does not exist. Creating... ", Color::YELLOW);
158
            }
159
            $runningBalance = new RunningBalance();
160
            $runningBalance->setUser($user);
161
162
            $this->objectManager->persist($runningBalance);
163
164
        } else {
165
            if ($console) {
166
                $console->writeLine("Running Balance row exists. Skipping", Color::YELLOW);
167
            }
168
        }
169
    }
170
171
    /**
172
     * @return array
173
     */
174
    public function getErrors()
175
    {
176
        return $this->errors;
177
    }
178
}
179