GuzzleHttpClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 52
c 0
b 0
f 0
wmc 4
lcom 1
cbo 6
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getResponse() 0 20 3
1
<?php
2
3
namespace HappyHourReminder\Client;
4
5
use GuzzleHttp\Client;
6
use HappyHourReminder\Entity\Response;
7
use Symfony\Component\Cache\Adapter\AbstractAdapter;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
/**
11
 * Class GuzzleHttpClient
12
 *
13
 * @package HappyHourReminder\Client
14
 */
15
class GuzzleHttpClient implements ClientInterface
16
{
17
    const CHECK_URL = 'https://www.bankmillennium.pl/klienci-indywidualni/produkty-oszczednosciowe/lokaty/happy-hours';
18
    const LAST_CONTENT_CACHE_KEY = 'happyhour.last.content';
19
20
    /** @var Client */
21
    protected $httpClient;
22
23
    /** @var Crawler */
24
    protected $crawler;
25
26
    /** @var AbstractAdapter */
27
    protected $cache;
28
29
    /**
30
     * GuzzleHttpClient constructor.
31
     *
32
     * @param Client          $httpClient
33
     * @param Crawler         $crawler
34
     * @param AbstractAdapter $cache
35
     */
36
    public function __construct(Client $httpClient, Crawler $crawler, AbstractAdapter $cache)
37
    {
38
        $this->httpClient = $httpClient;
39
        $this->crawler    = $crawler;
40
        $this->cache      = $cache;
41
    }
42
    
43
    /**
44
     * @inheritDoc
45
     */
46
    public function getResponse()
47
    {
48
        $request = $this->httpClient->get(self::CHECK_URL);
49
50
        $this->crawler->addHtmlContent($request->getBody());
51
52
        $lastContent    = $this->cache->getItem(self::LAST_CONTENT_CACHE_KEY);
53
        $currentContent = $this->crawler->filter('strong')->getNode(0)->textContent;
54
55
        if (!$lastContent->isHit()) {
56
            $lastContent->set($this->crawler->filter('strong')->getNode(0)->textContent);
57
            $this->cache->save($lastContent);
58
        } elseif ($lastContent->get() !== $currentContent) {
59
            $this->cache->deleteItem(self::LAST_CONTENT_CACHE_KEY);
60
61
            return new Response(true, $currentContent);
62
        }
63
64
        return new Response(false, null);
65
    }
66
}
67