Passed
Push — master ( dec909...4437a6 )
by Chris
10:44
created

CanRecognizeVideos   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 30
c 1
b 0
f 0
dl 0
loc 89
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A startDetectingLabels() 0 15 2
A setVideoSettings() 0 29 2
A startDetectingFaces() 0 15 2
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 the detection of labels/objects in a 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 startDetectingLabels(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 int $mediaId
77
     * @param array $attributes
78
     * @return \Aws\Result
79
     * @throws \Exception
80
     */
81
    public function startDetectingFaces($mediaId = null, $attributes = ['DEFAULT'])
0 ignored issues
show
Unused Code introduced by
The parameter $attributes is not used and could be removed. ( Ignorable by Annotation )

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

81
    public function startDetectingFaces($mediaId = null, /** @scrutinizer ignore-unused */ $attributes = ['DEFAULT'])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
87
        $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...
Comprehensibility Best Practice introduced by
The variable $minConfidence seems to never exist and therefore isset should always be false.
Loading history...
88
89
        $results = $this->client->startFaceDetection($this->settings);
90
91
        if ($results['JobId']) {
92
            $this->updateJobId($results['JobId'], 'faces');
93
        }
94
95
        return $results;
96
    }
97
}
98