Passed
Push — master ( 90e201...b182a5 )
by Matt
05:14
created

ImaggaService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 59
rs 10
c 2
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A tagImage() 0 29 5
A __construct() 0 18 1
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\Image;
6
use Doctrine\ORM\EntityManagerInterface;
7
use ErrorException;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\HandlerStack;
10
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;
11
12
class ImaggaService
13
{
14
    /** @var Client */
15
    private $guzzle;
16
17
    /** @var EntityManagerInterface */
18
    private $entityManager;
19
20
    public function __construct(
21
        string $imaggaApiKey,
22
        string $imaggaApiSecret,
23
        EntityManagerInterface $entityManager)
24
    {
25
        $this->entityManager = $entityManager;
26
27
        $stack = HandlerStack::create();
28
        // TODO: Parameterise rate limit
29
        $stack->push(RateLimiterMiddleware::perSecond(1));
30
31
        $this->guzzle = new Client([
32
            'handler' => $stack,
33
            // TODO: Parameterise URI
34
            'base_uri' => 'https://api.imagga.com',
35
            'auth' => [
36
                $imaggaApiKey,
37
                $imaggaApiSecret
38
            ]
39
        ]);
40
    }
41
42
    public function tagImage(Image $image, bool $overwriteExisting = false): bool
43
    {
44
        if ($overwriteExisting == false && $image->getAutoTagsCount() > 0) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
45
            return false;
46
        }
47
48
        $response = $this->guzzle->request('GET', '/v2/tags', [
49
            'query' => [
50
                //'image_url' => $image->getMediumImageUri(),
51
                'image_url' => 'https://omm.gothick.org.uk/media/cache/srcset_576/uploads/images/20210328-mg-9921-terry-house-6067172c952d1544478055.jpg',
52
                'threshold' => 15.0
53
            ]
54
        ]);
55
56
        $content = (string) $response->getBody();
57
        $imagga_result = json_decode($content);
58
59
        if ($imagga_result->status->type != 'success') {
60
            throw new ErrorException("Error returned from imagga: " . $imagga_result->status->text);
61
        }
62
63
        $tags = [];
64
        foreach($imagga_result->result->tags as $tag) {
65
            $tags[] = $tag->tag->en;
66
        }
67
        $image->setAutoTags($tags);
68
        $this->entityManager->persist($image);
69
        $this->entityManager->flush(); // Calling the API's a lot more overhead; we might as well flush on every image.
70
        return true;
71
    }
72
}
73