Passed
Push — master ( fba003...f4c767 )
by Chris
17:41 queued 02:40
created

CanRecognizeImages   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 137
rs 10
c 1
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A blob() 0 5 1
A setImageSettings() 0 23 3
A detectImageFaces() 0 11 1
A detectImageLabels() 0 15 2
A detectImageModeration() 0 11 1
A detectImageText() 0 13 2
1
<?php
2
3
namespace Meema\MediaRecognition\Traits;
4
5
use Exception;
6
7
trait CanRecognizeImages
8
{
9
    /**
10
     * The input image as base64-encoded bytes.
11
     *
12
     * @var string|null
13
     */
14
    protected ?string $blob = null;
15
16
    /**
17
     * Set the base64 encoded image.
18
     *
19
     * @param string $blob
20
     *
21
     * @return $this
22
     */
23
    public function blob(string $blob)
24
    {
25
        $this->blob = $blob;
26
27
        return $this;
28
    }
29
30
    /**
31
     * Sets the image to be analyzed.
32
     *
33
     * @return void
34
     * @throws \Exception
35
     */
36
    protected function setImageSettings(): void
37
    {
38
        $this->ensureSourceIsNotNull();
0 ignored issues
show
Bug introduced by
It seems like ensureSourceIsNotNull() 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

38
        $this->/** @scrutinizer ignore-call */ 
39
               ensureSourceIsNotNull();
Loading history...
39
40
        if (is_string($this->blob)) {
41
            $this->settings['Image'] = [
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...
42
                'Bytes' => $this->blob,
43
            ];
44
45
            return;
46
        }
47
48
        $disk = $this->disk ?? config('media-recognition.disk');
49
        $bucketName = config("filesystems.disks.$disk.bucket");
50
51
        if (! $bucketName) {
52
            throw new Exception('Please make sure to set a S3 bucket name.');
53
        }
54
55
        $this->settings['Image'] = [
56
            'S3Object' => [
57
                'Bucket' => $bucketName,
58
                'Name' => $this->source,
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * @param $mediaId
65
     * @param int|null $minConfidence
66
     * @param null $maxLabels
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $maxLabels is correct as it would always require null to be passed?
Loading history...
67
     * @return mixed
68
     * @throws \Exception
69
     */
70
    public function detectImageLabels($mediaId, $minConfidence = null, $maxLabels = null)
71
    {
72
        $this->setImageSettings();
73
74
        $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...
75
76
        if (is_int($maxLabels)) {
0 ignored issues
show
introduced by
The condition is_int($maxLabels) is always false.
Loading history...
77
            $this->settings['MaxLabels'] = $maxLabels;
78
        }
79
80
        $results = $this->client->detectLabels($this->settings);
81
82
        $this->updateOrCreate('labels', $mediaId, $results);
0 ignored issues
show
Bug introduced by
It seems like updateOrCreate() 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

82
        $this->/** @scrutinizer ignore-call */ 
83
               updateOrCreate('labels', $mediaId, $results);
Loading history...
83
84
        return $results;
85
    }
86
87
    /**
88
     * @param int|null $mediaId
89
     * @param array $attributes
90
     * @return mixed
91
     * @throws \Exception
92
     */
93
    public function detectImageFaces($mediaId = null, $attributes = ['DEFAULT'])
94
    {
95
        $this->setImageSettings();
96
97
        $this->settings['Attributes'] = $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...
98
99
        $results = $this->client->detectFaces($this->settings);
100
101
        $this->updateOrCreate('faces', $mediaId, $results);
102
103
        return $results;
104
    }
105
106
    /**
107
     * @param int|null $mediaId
108
     * @param int|null $minConfidence
109
     * @return mixed
110
     * @throws \Exception
111
     */
112
    public function detectImageModeration($mediaId = null, $minConfidence = null)
113
    {
114
        $this->setImageSettings();
115
116
        $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...
117
118
        $results = $this->client->detectModerationLabels($this->settings);
119
120
        $this->updateOrCreate('moderation', $mediaId, $results);
121
122
        return $results;
123
    }
124
125
    /**
126
     * @param int|null $mediaId
127
     * @param array|null $filters
128
     * @return mixed
129
     * @throws \Exception
130
     */
131
    public function detectImageText($mediaId = null, array $filters = null)
132
    {
133
        $this->setImageSettings();
134
135
        if (is_array($filters)) {
136
            $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...
137
        }
138
139
        $results = $this->client->detectText($this->settings);
140
141
        $this->updateOrCreate('ocr', $mediaId, $results);
142
143
        return $results;
144
    }
145
}
146