Passed
Push — master ( c76e66...6ccefa )
by Chris
11:36
created

Rekognition   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 29
c 6
b 2
f 0
dl 0
loc 107
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getClient() 0 3 1
A updateOrCreate() 0 16 3
A updateJobId() 0 14 3
A updateVideoResults() 0 9 2
1
<?php
2
3
namespace Meema\MediaRecognition\Recognizers;
4
5
use Aws\Rekognition\RekognitionClient;
6
use Exception;
7
use Meema\MediaRecognition\Contracts\MediaRecognition as MediaRecognitionInterface;
8
use Meema\MediaRecognition\Models\MediaRecognition;
9
use Meema\MediaRecognition\Traits\CanRecognizeImages;
10
use Meema\MediaRecognition\Traits\CanRecognizeVideos;
11
use Meema\MediaRecognition\Traits\InteractsWithStorage;
12
13
class Rekognition implements MediaRecognitionInterface
14
{
15
    use InteractsWithStorage, CanRecognizeImages, CanRecognizeVideos;
16
17
    /**
18
     * Client instance of MediaRecognition.
19
     *
20
     * @var \Aws\Rekognition\RekognitionClient
21
     */
22
    protected RekognitionClient $client;
23
24
    /**
25
     * The settings provided to the Rekognition job.
26
     *
27
     * @var array
28
     */
29
    protected array $settings;
30
31
    /**
32
     * The relating media model's id.
33
     *
34
     * @var int|null
35
     */
36
    protected ?int $mediaId = null;
37
38
    /**
39
     * Construct converter.
40
     *
41
     * @param \Aws\Rekognition\RekognitionClient $client
42
     */
43
    public function __construct(RekognitionClient $client)
44
    {
45
        $this->client = $client;
46
    }
47
48
    /**
49
     * Get the MediaRecognition Client.
50
     *
51
     * @return \Aws\Rekognition\RekognitionClient
52
     */
53
    public function getClient(): RekognitionClient
54
    {
55
        return $this->client;
56
    }
57
58
    /**
59
     * @param $type
60
     * @param $mediaId
61
     * @param $results
62
     * @return mixed
63
     * @throws \Exception
64
     */
65
    protected function updateOrCreate($type, $mediaId, $results)
66
    {
67
        if (! config('media-recognition.track_media_recognitions')) {
68
            return $results;
69
        }
70
71
        if (is_null($mediaId)) {
72
            throw new Exception('Please make sure to set a $mediaId.');
73
        }
74
75
        MediaRecognition::updateOrCreate([
76
            'model_id' => $mediaId,
77
            'model_type' => config('media-converter.media_model'),
78
        ], [$type => $results->toArray()]);
79
80
        return $results;
81
    }
82
83
    /**
84
     * @param string $jobId
85
     * @param string $type
86
     * @return void
87
     * @throws \Exception
88
     */
89
    protected function updateJobId(string $jobId, string $type)
90
    {
91
        if (! config('media-recognition.track_media_recognitions')) {
92
            return;
93
        }
94
95
        if (is_null($this->mediaId)) {
96
            throw new Exception('Please make sure to set a $mediaId.');
97
        }
98
99
        MediaRecognition::updateOrCreate([
100
            'model_id' => $this->mediaId,
101
            'model_type' => config('media-converter.media_model'),
102
        ], [$type.'_job_id' => $jobId]);
103
    }
104
105
    /**
106
     * @param array $results
107
     * @param string $type
108
     * @param int $mediaId
109
     * @return void
110
     */
111
    protected function updateVideoResults(array $results, string $type, int $mediaId)
112
    {
113
        if (! config('media-recognition.track_media_recognitions')) {
114
            return;
115
        }
116
117
        $mediaRecognition = MediaRecognition::where('model_id', $mediaId)->firstOrFail();
118
        $mediaRecognition->$type = $results;
119
        $mediaRecognition->save();
120
    }
121
}
122