ServerModel::subtractResources()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace App\Model;
4
5
use App\Entity\ServerEntity;
6
7
class ServerModel extends ServerEntity
8
{
9
    /** @var VmachineModel[] */
10
    protected $vmArray = [];
11
12
    /**
13
     * @return VmachineModel[]
14
     */
15
    public function getVmArray(): array
16
    {
17
        return $this->vmArray;
18
    }
19
20
    /**
21
     * Subtract resources from given machine.
22
     *
23
     * @param VmachineModel $vmachine
24
     *
25
     * @return $this
26
     */
27
    public function subtractResources(VmachineModel $vmachine)
28
    {
29
        $this->getCpu()->subtract($vmachine->getCpu()->getAmount());
30
        $this->getRam()->subtract($vmachine->getRam()->getAmount());
31
        $this->getHdd()->subtract($vmachine->getHdd()->getAmount());
32
33
        return $this;
34
    }
35
36
    /**
37
     * if resources are greater than given.
38
     *
39
     * @param ServerModel $vmachine
40
     *
41
     * @return bool
42
     */
43
    public function isGreaterThan(ServerModel $vmachine)
44
    {
45
        return $this->getCpu()->getAmount() > $vmachine->getCpu()->getAmount()
46
            || $this->getRam()->getAmount() > $vmachine->getRam()->getAmount()
47
            || $this->getHdd()->getAmount() > $vmachine->getHdd()->getAmount();
48
    }
49
50
    /**
51
     * String output of VM.
52
     *
53
     * @return string
54
     */
55
    public function toString()
56
    {
57
        return 'VM('
58
            . $this->getCpu()->toString() . ', '
59
            . $this->getRam()->toString() . ', '
60
            . $this->getHdd()->toString() . ')';
61
    }
62
63
    /**
64
     * Add VM to the server.
65
     *
66
     * @param VMachineModel $vmachine
67
     *
68
     * @return $this
69
     */
70
    public function addVm(VMachineModel $vmachine)
71
    {
72
        $this->vmArray[] = $vmachine;
73
        $this->subtractResources($vmachine);
74
75
        return $this;
76
    }
77
}
78