1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* ServerFarm consists of servers used to host virtual machines. |
7
|
|
|
*/ |
8
|
|
|
class ServerFarmModel |
9
|
|
|
{ |
10
|
|
|
/** @var ServerModel[] */ |
11
|
|
|
protected $servers = []; |
12
|
|
|
/** @var ServerModel */ |
13
|
|
|
protected $max; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ServerFarm constructor. |
17
|
|
|
* |
18
|
|
|
* @param ServerModel $max |
19
|
|
|
*/ |
20
|
|
|
public function __construct(ServerModel $max) |
21
|
|
|
{ |
22
|
|
|
$this->max = $max; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return ServerModel[] |
27
|
|
|
*/ |
28
|
|
|
public function getServers(): array |
29
|
|
|
{ |
30
|
|
|
return $this->servers; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add Virtual machine to the farm. |
35
|
|
|
* |
36
|
|
|
* @param VmachineModel $vmachine |
37
|
|
|
*/ |
38
|
|
|
public function addVm(VmachineModel $vmachine) |
39
|
|
|
{ |
40
|
|
|
if ($vmachine->isGreaterThan($this->max)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
foreach ($this->getServers() as $server) { |
44
|
|
|
if (!$vmachine->isGreaterThan($server)) { |
45
|
|
|
$server->addVm($vmachine); |
46
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->servers[] = ServerModel::createAsVm($this->max)->addVm($vmachine); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get number of servers needed to store given array of VMs. |
55
|
|
|
* |
56
|
|
|
* @param VMachineModel[] $vmArray |
57
|
|
|
* |
58
|
|
|
* @return ServerModel[] |
59
|
|
|
*/ |
60
|
|
|
public function serversNeededForVmachineArray(array $vmArray) |
61
|
|
|
{ |
62
|
|
|
foreach ($vmArray as $vmachine) { |
63
|
|
|
$this->addVm($vmachine); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->getServers(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Store given array of VMs. |
71
|
|
|
* |
72
|
|
|
* @param VMachineModel[] $vmArray |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function storeVmachines(array $vmArray) |
77
|
|
|
{ |
78
|
|
|
foreach ($vmArray as $vmachine) { |
79
|
|
|
$this->addVm($vmachine); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Output allocation. |
87
|
|
|
*/ |
88
|
|
|
public function toString() |
89
|
|
|
{ |
90
|
|
|
$counter = 1; |
91
|
|
|
$output = 'Server list' . PHP_EOL; |
92
|
|
|
foreach ($this->servers as $server) { |
93
|
|
|
$output .= $counter++ . '. '; |
94
|
|
|
foreach ($server->getVmArray() as $vmachine) { |
95
|
|
|
$output .= $vmachine->toString() . ' '; |
96
|
|
|
} |
97
|
|
|
$output .= 'remains ' . $server->toString(); |
98
|
|
|
$output .= PHP_EOL; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $output; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function count() |
105
|
|
|
{ |
106
|
|
|
return count($this->servers); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|