Passed
Push — master ( 198526...2cbb41 )
by Chris
13:39
created

CanRecognizeVideos::detectVideoLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 15
rs 10
1
<?php
2
3
namespace Meema\MediaRecognition\Traits;
4
5
use Exception;
6
7
trait CanRecognizeVideos
8
{
9
    /**
10
     * Sets the video to be analyzed.
11
     *
12
     * @param $type - used to create a readable identifier.
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
13
     * @return void
14
     * @throws \Exception
15
     */
16
    protected function setVideoSettings($type): void
17
    {
18
        $disk = $this->disk ?? config('media-recognition.disk');
19
        $bucketName = config("filesystems.disks.$disk.bucket");
20
21
        if (! $bucketName) {
22
            throw new Exception('Please make sure to set a S3 bucket name.');
23
        }
24
25
        $this->settings['Video'] = [
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
            'S3Object' => [
27
                'Bucket' => $bucketName,
28
                'Name' => $this->source,
29
            ],
30
        ];
31
32
        $this->settings['NotificationChannel'] = [
33
            'RoleArn' => config('media-recognition.iam_arn'),
34
            'SNSTopicArn' => config('media-recognition.sns_topic_arn'),
35
        ];
36
37
        $uniqueId = $type.'_'.$this->mediaId;
38
        // Idempotent token used to identify the start request.
39
        // If you use the same token with multiple StartCelebrityRecognition requests, the same JobId is returned.
40
        // Use ClientRequestToken to prevent the same job from being accidentally started more than once.
41
        $this->settings['ClientRequestToken'] = $uniqueId;
42
43
        // the JobTag is set to be the media id, so we can adjust the media record with the results once the webhook comes in
44
        $this->settings['JobTag'] = $uniqueId;
45
    }
46
47
    /**
48
     * Starts asynchronous detection of labels/objects in a stored video.
49
     *
50
     * @param int|null $mediaId
51
     * @param int|null $minConfidence
52
     * @param int $maxResults
53
     * @return \Aws\Result
54
     * @throws \Exception
55
     */
56
    public function detectVideoLabels($mediaId = null, $minConfidence = null, $maxResults = 1000)
57
    {
58
        $this->mediaId = $mediaId;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
60
        $this->setVideoSettings('labels');
61
        $this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
        $this->settings['MaxResults'] = $maxResults;
63
64
        $results = $this->client->startLabelDetection($this->settings);
65
66
        if ($results['JobId']) {
67
            $this->updateJobId($results['JobId'], 'labels');
0 ignored issues
show
Bug introduced by
It seems like updateJobId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            $this->/** @scrutinizer ignore-call */ 
68
                   updateJobId($results['JobId'], 'labels');
Loading history...
68
        }
69
70
        return $results;
71
    }
72
73
    /**
74
     * @param int|null $mediaId
75
     * @param array $attributes
76
     * @return mixed
77
     * @throws \Exception
78
     */
79
    public function detectVideoFaces($mediaId = null, $attributes = ['DEFAULT'])
80
    {
81
        $this->mediaId = $mediaId;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
83
        $this->setVideoSettings('faces');
84
        $this->settings['FaceAttributes'] = $attributes;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
86
        $results = $this->client->startFaceDetection($this->settings);
87
88
        if ($results['JobId']) {
89
            $this->updateJobId($results['JobId'], 'faces');
90
        }
91
92
        return $results;
93
    }
94
95
    /**
96
     * Starts the detection of faces in a video.
97
     *
98
     * @param null $mediaId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mediaId is correct as it would always require null to be passed?
Loading history...
99
     * @param string $faceAttribute
100
     * @return \Aws\Result
101
     * @throws \Exception
102
     */
103
    public function startFaceDetection($mediaId = null, string $faceAttribute = 'DEFAULT')
104
    {
105
        $this->mediaId = $mediaId;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
107
        $this->setVideoSettings('faces');
108
        $this->settings['FaceAttributes'] = $faceAttribute;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
110
        $results = $this->client->startFaceDetection($this->settings);
111
112
        if ($results['JobId']) {
113
            $this->updateJobId($results['JobId'], 'faces');
114
        }
115
116
        return $results;
117
    }
118
119
    /**
120
     * Starts asynchronous detection of unsafe content in a stored video.
121
     *
122
     * @param null $mediaId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mediaId is correct as it would always require null to be passed?
Loading history...
123
     * @param int|null $minConfidence
124
     * @return \Aws\Result
125
     * @throws \Exception
126
     */
127
    public function startContentModeration($mediaId = null, $minConfidence = null)
128
    {
129
        $this->mediaId = $mediaId;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
130
131
        $this->setVideoSettings('moderation');
132
133
        $this->settings['MinConfidence'] = $minConfidence ?? config('media-recognition.min_confidence');
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
135
        $results = $this->client->startContentModeration($this->settings);
136
137
        if ($results['JobId']) {
138
            $this->updateJobId($results['JobId'], 'faces');
139
        }
140
141
        return $results;
142
    }
143
144
    /**
145
     * Starts asynchronous detection of text in a stored video.
146
     *
147
     * @param null $mediaId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $mediaId is correct as it would always require null to be passed?
Loading history...
148
     * @param array|null $filters
149
     * @return \Aws\Result
150
     * @throws \Exception
151
     */
152
    public function startTextDetection($mediaId = null, array $filters = null)
153
    {
154
        $this->mediaId = $mediaId;
0 ignored issues
show
Bug Best Practice introduced by
The property mediaId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
155
156
        $this->setVideoSettings('ocr');
157
158
        if (is_array($filters)) {
159
            $this->settings['Filters'] = $filters;
0 ignored issues
show
Bug Best Practice introduced by
The property settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
160
        }
161
162
        $results = $this->client->startTextDetection($this->settings);
163
164
        if ($results['JobId']) {
165
            $this->updateJobId($results['JobId'], 'ocr');
166
        }
167
168
        return $results;
169
    }
170
171
    /**
172
     * Get the labels from the video analysis.
173
     *
174
     * @param string $jobId
175
     * @param int $mediaId
176
     * @return \Aws\Result
177
     * @throws \Exception
178
     */
179
    public function getLabelsByJobId(string $jobId, int $mediaId)
180
    {
181
        $results = $this->client->getLabelDetection([
182
            'JobId' => $jobId,
183
        ]);
184
185
        $this->updateVideoResults($results->toArray(), 'labels', $mediaId);
0 ignored issues
show
Bug introduced by
It seems like updateVideoResults() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
        $this->/** @scrutinizer ignore-call */ 
186
               updateVideoResults($results->toArray(), 'labels', $mediaId);
Loading history...
186
187
        return $results;
188
    }
189
190
    /**
191
     * Get the faces from the video analysis.
192
     *
193
     * @param string $jobId
194
     * @param int $mediaId
195
     * @return \Aws\Result
196
     * @throws \Exception
197
     */
198
    public function getFacesByJobId(string $jobId, int $mediaId)
199
    {
200
        $results = $this->client->getFaceDetection([
201
            'JobId' => $jobId,
202
        ]);
203
204
        $this->updateVideoResults($results->toArray(), 'faces', $mediaId);
205
206
        return $results;
207
    }
208
209
    /**
210
     * Get the "content moderation" from the video analysis.
211
     *
212
     * @param string $jobId
213
     * @param int $mediaId
214
     * @return \Aws\Result
215
     * @throws \Exception
216
     */
217
    public function getContentModerationByJobId(string $jobId, int $mediaId)
218
    {
219
        $results = $this->client->getContentModeration([
220
            'JobId' => $jobId,
221
        ]);
222
223
        $this->updateVideoResults($results->toArray(), 'moderation', $mediaId);
224
225
        return $results;
226
    }
227
228
    /**
229
     * Get the faces from a video analysis.
230
     *
231
     * @param string $jobId
232
     * @param int $mediaId
233
     * @return \Aws\Result
234
     * @throws \Exception
235
     */
236
    public function getTextDetectionByJobId(string $jobId, int $mediaId)
237
    {
238
        $results = $this->client->getTextDetection([
239
            'JobId' => $jobId,
240
        ]);
241
242
        $this->updateVideoResults($results->toArray(), 'ocr', $mediaId);
243
244
        return $results;
245
    }
246
}
247