Completed
Push — master ( 791aba...50879e )
by Timur
01:32
created

ListBackupsCommand::createResourcesFromJsonData()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 3
1
<?php
2
3
namespace Linode\Api\Backups\Commands;
4
5
use Linode\Api\Backups\Backup;
6
use Psr\Http\Message\ResponseInterface;
7
use Zurbaev\ApiClient\Commands\ListResourcesCommand;
8
use Zurbaev\ApiClient\Contracts\ApiResourceInterface;
9
10
class ListBackupsCommand extends ListResourcesCommand
11
{
12
    protected function itemsKey()
13
    {
14
        return 'backups';
15
    }
16
17
    public function resourcePath()
18
    {
19
        return 'backups';
20
    }
21
22
    public function resourceClass()
23
    {
24
        return Backup::class;
25
    }
26
27
    protected function createResourcesFromJsonData(array $data, ResponseInterface $response, ApiResourceInterface $owner)
28
    {
29
        $items = [];
30
        $className = $this->resourceClass();
31
32
        foreach ($data as $datum) {
33
            $types = [
34
                'daily' => $datum['daily'] ?? null,
35
                'weekly' => $datum['weekly'] ?? null,
36
                'current_snapshot' => $datum['snapshot']['current'] ?? null,
37
                'in_progress_snapshot' => $datum['snapshot']['in_progress'] ?? null,
38
            ];
39
40
            foreach ($types as $type => $item) {
41
                if (is_null($item)) {
42
                    continue;
43
                }
44
45
                $item['backup_type'] = $type;
46
47
                $items[] = new $className($owner->getApi(), $item, $owner);
48
            }
49
        }
50
51
        return $items;
52
    }
53
}
54