ServerHistory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Monitor\Service;
3
4
use Monitor\Model\ServerHistory as ServerHistoryModel;
5
use Doctrine\ORM\EntityManager;
6
7
class ServerHistory
8
{
9
10
    /**
11
     * @var \Doctrine\ORM\EntityManager
12
     */
13
    private $em;
14
15
    public function __construct(EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. 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...
16
    {
17
        $this->em = $em;
18
    }
19
20
    /**
21
     * Save server history
22
     *
23
     * @param \Monitor\Model\ServerHistory $serverHistory
24
     */
25
    public function save(ServerHistoryModel $serverHistory)
26
    {
27
        $this->em->persist($serverHistory);
28
        $this->em->flush();
29
    }
30
31
    /**
32
     * Delete old server history records by specifing time
33
     *
34
     * @param int $time
35
     */
36
    public function deleteRecordsByTime($time)
37
    {
38
        $query = $this->em
39
            ->createQuery('DELETE from \Monitor\Model\ServerHistory s where s.time < ?1');
40
        $query->setParameter('1', $time);
41
        $query->execute();
42
    }
43
44
    /**
45
     * Get 'servers_history' table structure
46
     *
47
     * @return array $properties
48
     */
49
    public function getTableStructure()
50
    {
51
        $reflection = new \ReflectionClass(new \Monitor\Model\ServerHistory);
52
        $properties = [];
53
        foreach ($reflection->getProperties() as $property) {
54
            $properties[] = $property->name;
55
        }
56
        unset($properties['id']);
57
        return $properties;
58
    }
59
60
    /**
61
     * Store server history
62
     *
63
     * @param array $server
64
     */
65
    public function addServerHistory(array $server)
66
    {
67
        $serverHistory = new ServerHistoryModel;
68
        $serverHistory->setServerId($server['server_id']);
69
        $serverHistory->setHostname($server['hostname']);
70
        $serverHistory->setStatus($server['status']);
71
        $serverHistory->setSysLoad($server['sys_load']);
72
        $serverHistory->setCpuCores($server['cpu_cores']);
73
        $serverHistory->setMemoryUsage($server['memory_usage']);
74
        $serverHistory->setMemoryTotal($server['memory_total']);
75
        $serverHistory->setMemoryFree($server['memory_free']);
76
        $serverHistory->setDiskFree($server['disk_free']);
77
        $serverHistory->setDiskTotal($server['disk_total']);
78
        $serverHistory->setDiskUsage($server['disk_usage']);
79
        $serverHistory->setPing($server['ping']);
80
        $serverHistory->setMysqlSlowQuery($server['mysql_slow_query']);
81
        $serverHistory->setMysqlQueryAvg($server['mysql_query_avg']);
82
        $serverHistory->setMemcacheHits($server['memcache_hits']);
83
        $serverHistory->setMemcacheMiss($server['memcache_miss']);
84
        $serverHistory->setMemcacheGet($server['memcache_get']);
85
        $serverHistory->setMemcacheCmd($server['memcache_cmd']);
86
        $serverHistory->setMemcacheBytes($server['memcache_bytes']);
87
        $serverHistory->setMemcacheMaxBytes($server['memcache_max_bytes']);
88
        $serverHistory->setTime(time());
89
        $this->save($serverHistory);
90
    }
91
}
92