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

StartFaceDetection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Meema\MediaRecognition\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Queue\SerializesModels;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Support\Str;
12
use Meema\MediaRecognition\Events\FacialAnalysisCompleted;
13
use Meema\MediaRecognition\Facades\Recognize;
14
15
class StartFaceDetection implements ShouldQueue
16
{
17
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by Meema\MediaRecognition\Jobs\StartFaceDetection: $id, $relations, $class, $keyBy
Loading history...
18
19
    private string $path;
20
21
    /**
22
     * $mimeType may be 'image', 'video' or the actual mime type of the file.
23
     * It allows us to reduce an HTTP request to check for the mime type.
24
     * If not assigned, it will check the mime type for whether it is an image or a video source.
25
     *
26
     * @var string|null
27
     */
28
    private ?string $mimeType;
29
30
    private ?int $mediaId;
31
32
    private array $faceAttribute;
33
34
    /**
35
     * Create a new job instance.
36
     *
37
     * @param string $path
38
     * @param string|null $mimeType
39
     * @param int|null $mediaId
40
     * @param string $faceAttribute
41
     */
42
    public function __construct(string $path, $mimeType = null, $mediaId = null, $faceAttribute = 'DEFAULT')
43
    {
44
        $this->path = $path;
45
        $this->mimeType = $mimeType;
46
        $this->mediaId = $mediaId;
47
        $this->faceAttribute = [$faceAttribute];
48
    }
49
50
    /**
51
     * Execute the job.
52
     *
53
     * @return void
54
     * @throws \Exception
55
     */
56
    public function handle()
57
    {
58
        $this->ensureMimeTypeIsSet();
59
60
        if (Str::contains($this->mimeType, 'image')) {
61
            $result = Recognize::source($this->path, $this->mimeType)->detectImageFaces($this->mediaId, $this->faceAttribute);
0 ignored issues
show
Bug introduced by
The method detectImageFaces() does not exist on Meema\MediaRecognition\Contracts\MediaRecognition. Did you maybe mean detectImageLabels()? ( Ignorable by Annotation )

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

61
            $result = Recognize::source($this->path, $this->mimeType)->/** @scrutinizer ignore-call */ detectImageFaces($this->mediaId, $this->faceAttribute);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
63
            // we need to manually fire the event for image analyses because unlike the video analysis,
64
            // AWS is not sending a webhook upon completion of the image analysis
65
            event(new FacialAnalysisCompleted($result));
66
67
            return;
68
        }
69
70
        if (Str::contains($this->mimeType, 'video')) {
71
            Recognize::source($this->path, $this->mimeType)->detectVideoFaces($this->mediaId, $this->faceAttribute);
0 ignored issues
show
Bug introduced by
The method detectVideoFaces() does not exist on Meema\MediaRecognition\Contracts\MediaRecognition. Did you maybe mean detectVideoLabels()? ( Ignorable by Annotation )

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

71
            Recognize::source($this->path, $this->mimeType)->/** @scrutinizer ignore-call */ detectVideoFaces($this->mediaId, $this->faceAttribute);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
73
            return;
74
        }
75
76
        throw new \Exception('$mimeType does neither indicate being a video nor an image');
77
    }
78
79
    protected function ensureMimeTypeIsSet()
80
    {
81
        if (is_null($this->mimeType)) {
82
            $this->mimeType = Storage::disk(config('media-recognition.disk'))->mimeType($this->path);
0 ignored issues
show
Documentation Bug introduced by
It seems like Illuminate\Support\Facad...->mimeType($this->path) can also be of type false. However, the property $mimeType is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
83
        }
84
    }
85
}
86