|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2016 Francois-Xavier Soubirou. |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of eco4. |
|
7
|
|
|
* |
|
8
|
|
|
* eco4 is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU General Public License as published by |
|
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* eco4 is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU General Public License |
|
19
|
|
|
* along with eco4. If not, see <http://www.gnu.org/licenses/>. |
|
20
|
|
|
*/ |
|
21
|
|
|
declare(strict_types=1); |
|
22
|
|
|
|
|
23
|
|
|
namespace AppBundle\Service; |
|
24
|
|
|
|
|
25
|
|
|
use AppBundle\Entity\Event; |
|
26
|
|
|
use AppBundle\Entity\Mine; |
|
27
|
|
|
use AppBundle\Entity\ObjectType; |
|
28
|
|
|
use AppBundle\Entity\User; |
|
29
|
|
|
use AppBundle\Util\Period; |
|
30
|
|
|
use DateTime; |
|
31
|
|
|
use Doctrine\ORM\EntityManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Engine service class. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Eco4 App |
|
37
|
|
|
* |
|
38
|
|
|
* @author Francois-Xavier Soubirou <[email protected]> |
|
39
|
|
|
* @copyright 2016 Francois-Xavier Soubirou |
|
40
|
|
|
* @license http://www.gnu.org/licenses/ GPLv3 |
|
41
|
|
|
* |
|
42
|
|
|
* @link https://www.eco4.io |
|
43
|
|
|
*/ |
|
44
|
|
|
class MineService |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var EntityManager Entity manager |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $em; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param EntityManager $entityManager The entity manager |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(EntityManager $entityManager) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$this->em = $entityManager; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create a mine. |
|
63
|
|
|
* |
|
64
|
|
|
* @param DateTime $dateTime The datetime to update to |
|
65
|
|
|
* @param string $mineFactor Mine factor of production |
|
66
|
|
|
*/ |
|
67
|
|
|
public function create(DateTime $dateTime, string $mineFactor) |
|
68
|
|
|
{ |
|
69
|
|
|
$mine = new Mine(); |
|
70
|
|
|
$mine->setLevel(1); |
|
71
|
|
|
$mine->setR1(50); |
|
72
|
|
|
$mine->setR2(50); |
|
73
|
|
|
$mine->setR3(50); |
|
74
|
|
|
$mine->setFactor($mineFactor); |
|
75
|
|
|
$mine->setLastUpdate($dateTime); |
|
76
|
|
|
$mine->refresh($dateTime); |
|
77
|
|
|
|
|
78
|
|
|
$this->em->persist($mine); |
|
79
|
|
|
$this->em->flush(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Update mine. |
|
84
|
|
|
* |
|
85
|
|
|
* @param User $user The owner of the mine |
|
86
|
|
|
* @param DateTime $dateTime The datetime to update to |
|
87
|
|
|
*/ |
|
88
|
|
|
public function update(User $user, DateTime $dateTime) |
|
89
|
|
|
{ |
|
90
|
|
|
$mine = $user->getMine(); |
|
91
|
|
|
|
|
92
|
|
|
$period = new Period($user->getMine()->getLastUpdate(), $dateTime); |
|
93
|
|
|
|
|
94
|
|
|
$events = $this->em->getRepository(Event::class) |
|
95
|
|
|
->findPlannedEventByBuildingType4UserBetween($user, ObjectType::MINE, $period); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($events as $event) { |
|
98
|
|
|
if ($event->getStatus() == Event::STATUS_PLANNED) { |
|
99
|
|
|
$mine->refresh($event->getEventDatetime()); |
|
100
|
|
|
|
|
101
|
|
|
if ($event->getCategory() == Event::CAT_UPGRADE) { |
|
102
|
|
|
$mine->upgrade(); |
|
103
|
|
|
} |
|
104
|
|
|
$event->setStatus(Event::STATUS_DONE); |
|
105
|
|
|
$this->em->persist($event); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
$mine->refresh($dateTime); |
|
109
|
|
|
|
|
110
|
|
|
$this->em->persist($mine); |
|
111
|
|
|
$this->em->flush(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.