EngineService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A refreshAll() 0 10 2
A refresh() 0 10 2
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\Mine;
26
use AppBundle\Entity\ObjectType;
27
use AppBundle\Entity\User;
28
use DateTime;
29
use Doctrine\ORM\EntityManager;
30
31
/**
32
 * Engine service class.
33
 *
34
 * @category  Eco4 App
35
 *
36
 * @author    Francois-Xavier Soubirou <[email protected]>
37
 * @copyright 2016 Francois-Xavier Soubirou
38
 * @license   http://www.gnu.org/licenses/   GPLv3
39
 *
40
 * @link      https://www.eco4.io
41
 */
42
class EngineService
43
{
44
    /**
45
     * @var EntityManager Entity manager
46
     */
47
    protected $em;
48
49
    /**
50
     * @var MineService Mine service
51
     */
52
    protected $mineService;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param EntityManager $entityManager The entity manager
58
     * @param MineService   $mineService   The mine service
59
     */
60
    public function __construct(EntityManager $entityManager, MineService $mineService)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might 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:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
61
    {
62
        $this->em = $entityManager;
63
        $this->mineService = $mineService;
64
    }
65
66
    /**
67
     * Update all.
68
     */
69
    public function refreshAll()
70
    {
71
        $dateTime = new DateTime();
72
73
        $mines = $this->em->getRepository(Mine::class)->findAll();
74
75
        foreach ($mines as $mine) {
76
            $this->mineService->update($mine, $dateTime);
77
        }
78
    }
79
80
    /**
81
     * Update a building.
82
     *
83
     * @param User $user       The owner of the building
84
     * @param int  $objectType The building type
85
     */
86
    public function refresh(User $user, int $objectType)
87
    {
88
        $dateTime = new DateTime();
89
90
        switch ($objectType) {
91
            case ObjectType::MINE:
92
                $this->mineService->update($user, $dateTime);
93
                break;
94
        }
95
    }
96
}
97