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

CanRecognizeVideos::startTextDetection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 17
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->path,
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 $mediaId
51
     * @param int|null $minConfidence
52
     * @param int $maxResults
53
     * @return \Aws\Result
54
     * @throws \Exception
55
     */
56
    public function startLabelDetection(int $mediaId, $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
     * Starts the detection of faces in a video.
75
     *
76
     * @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...
77
     * @param string $faceAttribute
78
     * @return \Aws\Result
79
     * @throws \Exception
80
     */
81
    public function startFaceDetection($mediaId = null, string $faceAttribute = 'DEFAULT')
82
    {
83
        $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...
84
85
        $this->setVideoSettings('faces');
86
        $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...
87
88
        $results = $this->client->startFaceDetection($this->settings);
89
90
        if ($results['JobId']) {
91
            $this->updateJobId($results['JobId'], 'faces');
92
        }
93
94
        return $results;
95
    }
96
97
    /**
98
     * Starts asynchronous detection of unsafe content in a stored video.
99
     *
100
     * @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...
101
     * @param int|null $minConfidence
102
     * @return \Aws\Result
103
     * @throws \Exception
104
     */
105
    public function startContentModeration($mediaId = null, $minConfidence = null)
106
    {
107
        $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...
108
109
        $this->setVideoSettings('moderation');
110
111
        $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...
112
113
        $results = $this->client->startContentModeration($this->settings);
114
115
        if ($results['JobId']) {
116
            $this->updateJobId($results['JobId'], 'faces');
117
        }
118
119
        return $results;
120
    }
121
122
    /**
123
     * Starts asynchronous detection of text in a stored video.
124
     *
125
     * @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...
126
     * @param array|null $filters
127
     * @return \Aws\Result
128
     * @throws \Exception
129
     */
130
    public function startTextDetection($mediaId = null, array $filters = null)
131
    {
132
        $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...
133
134
        $this->setVideoSettings('ocr');
135
136
        if (is_array($filters)) {
137
            $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...
138
        }
139
140
        $results = $this->client->startTextDetection($this->settings);
141
142
        if ($results['JobId']) {
143
            $this->updateJobId($results['JobId'], 'ocr');
144
        }
145
146
        return $results;
147
    }
148
149
    /**
150
     * Get the labels from the video analysis.
151
     *
152
     * @param string $jobId
153
     * @param int $mediaId
154
     * @return \Aws\Result
155
     * @throws \Exception
156
     */
157
    public function getLabelsByJobId(string $jobId, int $mediaId)
158
    {
159
        $results = $this->client->getLabelDetection([
160
            'JobId' => $jobId,
161
        ]);
162
163
        $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

163
        $this->/** @scrutinizer ignore-call */ 
164
               updateVideoResults($results->toArray(),'labels', $mediaId);
Loading history...
164
165
        return $results;
166
    }
167
168
    /**
169
     * Get the faces from the video analysis.
170
     *
171
     * @param string $jobId
172
     * @param int $mediaId
173
     * @return \Aws\Result
174
     * @throws \Exception
175
     */
176
    public function getFacesByJobId(string $jobId, int $mediaId)
177
    {
178
        $results = $this->client->getFaceDetection([
179
            'JobId' => $jobId,
180
        ]);
181
182
        $this->updateVideoResults($results->toArray(),'faces', $mediaId);
183
184
        return $results;
185
    }
186
187
    /**
188
     * Get the "content moderation" from the video analysis.
189
     *
190
     * @param string $jobId
191
     * @param int $mediaId
192
     * @return \Aws\Result
193
     * @throws \Exception
194
     */
195
    public function getContentModerationByJobId(string $jobId, int $mediaId)
196
    {
197
        $results = $this->client->getContentModeration([
198
            'JobId' => $jobId,
199
        ]);
200
201
        $this->updateVideoResults($results->toArray(),'moderation', $mediaId);
202
203
        return $results;
204
    }
205
206
    /**
207
     * Get the faces from a video analysis.
208
     *
209
     * @param string $jobId
210
     * @param int $mediaId
211
     * @return \Aws\Result
212
     * @throws \Exception
213
     */
214
    public function getTextDetectionByJobId(string $jobId, int $mediaId)
215
    {
216
        $results = $this->client->getTextDetection([
217
            'JobId' => $jobId,
218
        ]);
219
220
        $this->updateVideoResults($results->toArray(),'ocr', $mediaId);
221
222
        return $results;
223
    }
224
}
225