QwerteeClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A feedItems() 0 17 2
A initializeFeed() 0 13 2
A extractData() 0 21 1
1
<?php
2
/**
3
 * QwerteeClient.php, qwertee-php.
4
 *
5
 * This File belongs to to Project qwertee-php
6
 *
7
 * @author Oliver Kaufmann <[email protected]>
8
 *
9
 * @version 1.0
10
 */
11
12
namespace Okaufmann\QwerteePhp;
13
14
use Illuminate\Support\Collection;
15
use Symfony\Component\DomCrawler\Crawler;
16
17
class QwerteeClient
18
{
19
    /**
20
     * @var \SimplePie
21
     */
22
    private $client;
23
24
    /**
25
     * @var string
26
     */
27
    private $feedUrl;
28
29
    public function __construct()
30
    {
31
        $this->feedUrl = 'https://www.qwertee.com/rss';
32
    }
33
34
    /**
35
     * Returns the actual feed items ordered by newest to oldest.
36
     *
37
     * @return \Illuminate\Support\Collection
38
     */
39
    public function feedItems()
40
    {
41
        if ($this->client == null) {
42
            $this->initializeFeed();
43
        }
44
45
        $list = collect($this->client->get_items())
46
            ->sortBy(function (\SimplePie_Item $item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
                return $item->get_title();
48
            })->sortByDesc(function (\SimplePie_Item $item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
                return $item->get_date('Ymd');
50
            });
51
52
        $list = $this->extractData($list);
53
54
        return $list;
55
    }
56
57
    /**
58
     * Setup SimplePie.
59
     *
60
     * @param null $data Set feed data otherwise the official qwertee feed url will be used.
61
     */
62
    public function initializeFeed($data = null)
63
    {
64
        $this->client = new \SimplePie();
65
66
        if ($data != null) {
67
            $this->client->set_raw_data($data);
68
        } else {
69
            $this->client->set_feed_url($this->feedUrl);
70
        }
71
72
        $this->client->enable_cache(false);
73
        $this->client->init();
74
    }
75
76
    private function extractData(Collection $feedItems)
77
    {
78
        $dataItems = $feedItems->map(function (\SimplePie_Item $item) {
79
            $crawler = new Crawler($item->get_content());
80
            $mens = $crawler->filter('img')->first()->attr('src');
81
82
            $keys = collect(['title', 'zoom', 'mens', 'womens', 'kids', 'hoodie', 'sweater', 'releasedAt']);
83
84
            $title = $item->get_title();
85
            $zoom = str_replace('mens', 'zoom', $mens);
86
            $womens = str_replace('mens', 'womens', $mens);
87
            $kids = str_replace('mens', 'kids', $mens);
88
            $hoodie = str_replace('mens', 'hoodie', $mens);
89
            $sweater = str_replace('mens', 'sweater', $mens);
90
            $releasedAt = $item->get_date('Y-m-d');
91
92
            return $keys->combine([$title, $zoom, $mens, $womens, $kids, $hoodie, $sweater, $releasedAt]);
93
        });
94
95
        return $dataItems;
96
    }
97
}
98