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

CanRecognizeImages::setImageSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 23
rs 9.8333
1
<?php
2
3
namespace Meema\MediaRecognition\Traits;
4
5
use Exception;
6
use Meema\MediaRecognition\Models\MediaRecognition;
7
8
trait CanRecognizeImages
9
{
10
    /**
11
     * The input image as base64-encoded bytes.
12
     *
13
     * @var string|null
14
     */
15
    protected ?string $blob = null;
16
17
    /**
18
     * Set the base64 encoded image.
19
     *
20
     * @param string $blob
21
     *
22
     * @return $this
23
     */
24
    public function blob(string $blob)
25
    {
26
        $this->blob = $blob;
27
28
        return $this;
29
    }
30
31
    /**
32
     * Sets the image to be analyzed.
33
     *
34
     * @return void
35
     * @throws \Exception
36
     */
37
    protected function setImageSettings(): void
38
    {
39
        $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

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

83
        $this->/** @scrutinizer ignore-call */ 
84
               updateOrCreate('labels', $mediaId, $results);
Loading history...
84
85
        return $results;
86
    }
87
88
    /**
89
     * @param int|null $mediaId
90
     * @param array $attributes
91
     * @return mixed
92
     * @throws \Exception
93
     */
94
    public function detectImageFaces($mediaId = null, $attributes = ['DEFAULT'])
95
    {
96
        $this->setImageSettings();
97
98
        $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...
99
100
        $results = $this->client->detectFaces($this->settings);
101
102
        $this->updateOrCreate('faces', $mediaId, $results);
103
104
        return $results;
105
    }
106
}
107