Passed
Push — master ( cefb78...c2816e )
by Chris
10:44
created

StartFaceDetection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 24 4
A __construct() 0 6 1
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
     * $mediaType may be 'image' or 'video' and it allows us to reduce an HTTP request to check for the mime type.
23
     * If not assigned, it will check the mime type for whether it is an image or a video source.
24
     *
25
     * @var string|null
26
     */
27
    private ?string $mediaType;
28
29
    private ?int $mediaId;
30
31
    private array $faceAttribute;
32
33
    /**
34
     * Create a new job instance.
35
     *
36
     * @param string $path
37
     * @param string|null $mediaType
38
     * @param int|null $mediaId
39
     * @param string $faceAttribute
40
     */
41
    public function __construct(string $path, $mediaType = null, $mediaId = null, $faceAttribute = 'DEFAULT')
42
    {
43
        $this->path = $path;
44
        $this->mediaId = $mediaId;
45
        $this->faceAttribute = [$faceAttribute];
46
        $this->mediaType = $mediaType;
47
    }
48
49
    /**
50
     * Execute the job.
51
     *
52
     * @return void
53
     * @throws \Exception
54
     */
55
    public function handle()
56
    {
57
        if (is_null($this->mediaType)) {
58
            $mimeType = Storage::disk(config('media-recognition.disk'))->mimeType($this->path);
59
        } else {
60
            $mimeType = $this->mediaType;
61
        }
62
63
        if (Str::contains($mimeType, 'image')) {
0 ignored issues
show
Bug introduced by
It seems like $mimeType can also be of type false; however, parameter $haystack of Illuminate\Support\Str::contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
        if (Str::contains(/** @scrutinizer ignore-type */ $mimeType, 'image')) {
Loading history...
64
            $result = Recognize::source($this->path)->detectFaces($this->mediaId, $this->faceAttribute);
65
66
            // we need to manually fire the event for image analyses because unlike the video analysis, AWS is not sending a webhook upon completion
67
            event(new FacialAnalysisCompleted($result));
68
69
            return;
70
        }
71
72
        if (Str::contains($mimeType, 'video')) {
73
            Recognize::source($this->path)->detectFaces($this->mediaId, $this->faceAttribute);
74
75
            return;
76
        }
77
78
        throw new \Exception('$mimeType does neither indicate being a video nor an image');
79
    }
80
}
81