Issues (35)

src/Traits/Recognizable.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Meema\MediaRecognition\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use Meema\MediaRecognition\Facades\Recognize;
7
use Meema\MediaRecognition\Models\MediaRecognition;
8
9
trait Recognizable
10
{
11
    /**
12
     * Get all of the media items' conversions.
13
     */
14
    public function recognition()
15
    {
16
        return $this->morphOne(MediaRecognition::class, 'model');
0 ignored issues
show
It seems like morphOne() 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

16
        return $this->/** @scrutinizer ignore-call */ morphOne(MediaRecognition::class, 'model');
Loading history...
17
    }
18
19
    /**
20
     * Start a media "recognition".
21
     *
22
     * @param string $path
23
     * @param string|null $mimeType
24
     * @return \Meema\MediaRecognition\Contracts\MediaRecognition
25
     */
26
    public function recognize(string $path, string $mimeType = null)
27
    {
28
        return Recognize::source($path, $mimeType, $this->id);
29
    }
30
31
    /**
32
     * Return all recognition data.
33
     * The return value "null" indicates that a recognition has been ran, but it just has no results.
34
     *
35
     * @return array
36
     */
37
    public function recognitionData()
38
    {
39
        $recognition = $this->recognition()->latest()->first();
40
41
        if (! $recognition) {
42
            return [
43
                'labels' => null,
44
                'faces' => null,
45
                'moderation' => null,
46
                'texts' => null,
47
            ];
48
        }
49
50
        // null indicates that the "recognition" has not been ran for the category
51
        $labels = $faces = $moderation = $texts = null;
52
53
        if ($recognition->labels && is_array($recognition->labels['Labels'])) {
54
            $labels = $recognition->labels['Labels'];
55
        }
56
57
        if ($recognition->faces && is_array($recognition->faces['FaceDetails'])) {
58
            $faces = $recognition->faces['FaceDetails'];
59
        }
60
61
        Log::info('before moderation');
62
        if ($recognition->moderation && is_array($recognition->moderation['ModerationLabels'])) {
63
            Log::info('after', $recognition->moderation['ModerationLabels']);
64
            $moderation = $recognition->moderation['ModerationLabels'];
65
        }
66
67
        if ($recognition->ocr && is_array($recognition->ocr['TextDetections'])) {
68
            $texts = $recognition->ocr['TextDetections'];
69
        }
70
71
        return [
72
            'labels' => $labels,
73
            'faces' => $faces,
74
            'moderation' => $moderation,
75
            'texts' => $texts,
76
        ];
77
    }
78
}
79