StatusFromDatabaseRetriever::convert()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 2
nc 2
nop 1
1
<?php
2
3
namespace W2w\Laravel\Apie\Plugins\Illuminate\DataLayers;
4
5
use ArrayIterator;
6
use Throwable;
7
use W2w\Laravel\Apie\Models\Status;
8
use W2w\Lib\Apie\Plugins\StatusCheck\ApiResources\Status as ResourceStatus;
9
use W2w\Lib\Apie\Plugins\StatusCheck\StatusChecks\StaticStatusCheck;
10
use W2w\Lib\Apie\Plugins\StatusCheck\StatusChecks\StatusCheckListInterface;
11
12
/**
13
 * Adds an extra check for the Status api resource to do a database check. Any record stored in the database
14
 * will be a status check report. Any background process can add/remove status checks.
15
 */
16
class StatusFromDatabaseRetriever implements StatusCheckListInterface
17
{
18
    private $debug;
19
20
    /**
21
     * @param bool $debug
22
     */
23
    public function __construct(bool $debug = false)
24
    {
25
        $this->debug = $debug;
26
    }
27
28
    /**
29
     * @return ArrayIterator
30
     */
31
    public function getIterator()
32
    {
33
        try {
34
            $statuses = Status::where([])->get();
35
        } catch (Throwable $t) {
36
            return new ArrayIterator(
37
                [
38
                new StaticStatusCheck(
39
                    new ResourceStatus(
40
                        'database-test',
41
                        'Can not connect to database',
42
                        null,
43
                        [
44
                            'exception' => $t->getMessage(),
45
                            'trace'     => $this->debug ? $t->getTraceAsString() : null,
46
                        ]
47
                    )
48
                ),
49
                ]
50
            );
51
        }
52
        $list = array_map(
53
            function (Status $statusModel) {
54
                return $this->convert($statusModel);
55
            }, iterator_to_array($statuses)
56
        );
57
        $list[] = new StaticStatusCheck(
58
            new ResourceStatus(
59
                'database-test',
60
                'OK'
61
            )
62
        );
63
64
        return new ArrayIterator($list);
65
    }
66
67
    /**
68
     * Converts a Eloquent Status into a StaticStatusCheck.
69
     *
70
     * @param  Status $statusModel
71
     * @return StaticStatusCheck
72
     */
73
    private function convert(Status $statusModel): StaticStatusCheck
74
    {
75
        $context = json_decode($statusModel->context, true);
76
        if (!is_array($context)) {
77
            $context = null;
78
        }
79
        $statusResource = new ResourceStatus(
80
            $statusModel->id,
81
            $statusModel->status,
82
            $statusModel->optional_reference,
83
            $context
84
        );
85
86
        return new StaticStatusCheck($statusResource);
87
    }
88
}
89