WallpaperList   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 134
Duplicated Lines 3.73 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 17
c 3
b 0
f 2
lcom 1
cbo 5
dl 5
loc 134
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 4 1
A offsetGet() 0 4 2
A offsetUnset() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
B downloadAll() 5 39 6
A offsetSet() 0 12 3
A addAll() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Wallhaven;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Pool;
7
use Wallhaven\Exceptions\DownloadException;
8
use Wallhaven\Exceptions\WallhavenException;
9
10
/**
11
 * Wallpaper list.
12
 *
13
 * @package Wallhaven
14
 */
15
class WallpaperList implements \ArrayAccess, \IteratorAggregate, \Countable
16
{
17
18
    /**
19
     * @var Wallpaper[] Wallpapers.
20
     */
21
    private $wallpapers = [];
22
23
    /**
24
     * Download all wallpapers in list.
25
     *
26
     * @param string $directory Where to download wallpapers.
27
     *
28
     * @throws DownloadException Thrown if the download directory cannot be created.
29
     */
30
    public function downloadAll($directory)
31
    {
32 View Code Duplication
        if (!file_exists($directory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            if (!@mkdir($directory, null, true)) {
34
                throw new DownloadException("The download directory cannot be created.");
35
            }
36
        }
37
38
        $client = new Client();
39
40
        $requests = [];
41
        foreach ($this->wallpapers as $w) {
42
            $url = $w->getImageUrl(true);
43
44
            $requests[] = $client->createRequest('GET', $url, [
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\Client. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
45
                'save_to' => $directory . '/' . basename($url)
46
            ]);
47
        }
48
49
        $results = Pool::batch($client, $requests);
50
51
        // Retry with PNG
52
        $retryRequests = [];
53
        foreach ($results->getFailures() as $e) {
0 ignored issues
show
Bug introduced by
The method getFailures cannot be called on $results (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
54
            // Delete failed files
55
            unlink($directory . '/' . basename($e->getRequest()->getUrl()));
56
57
            $urlPng = str_replace('.jpg', '.png', $e->getRequest()->getUrl());
58
            $statusCode = $e->getResponse()->getStatusCode();
59
60
            if ($statusCode == 404) {
61
                $retryRequests[] = $client->createRequest('GET', $urlPng, [
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\Client. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
                    'save_to' => $directory . '/' . basename($urlPng)
63
                ]);
64
            }
65
        }
66
67
        Pool::batch($client, $retryRequests);
68
    }
69
70
71
    /**
72
     *
73
     * @param $offset
74
     *
75
     * @return bool
76
     */
77
    public function offsetExists($offset)
78
    {
79
        return isset($this->wallpapers[$offset]);
80
    }
81
82
    /**
83
     *
84
     * @param $offset
85
     *
86
     * @return null|Wallpaper
87
     */
88
    public function offsetGet($offset)
89
    {
90
        return isset($this->wallpapers[$offset]) ? $this->wallpapers[$offset] : null;
91
    }
92
93
    /**
94
     *
95
     * @param           $offset
96
     * @param Wallpaper $value
97
     *
98
     * @throws WallhavenException
99
     */
100
    public function offsetSet($offset, $value)
101
    {
102
        if (!$value instanceof Wallpaper) {
103
            throw new WallhavenException("Not a Wallpaper object.");
104
        }
105
106
        if (is_null($offset)) {
107
            $this->wallpapers[] = $value;
108
        } else {
109
            $this->wallpapers[$offset] = $value;
110
        }
111
    }
112
113
    /**
114
     * @param $offset
115
     */
116
    public function offsetUnset($offset)
117
    {
118
        unset($this->wallpapers[$offset]);
119
    }
120
121
    /**
122
     * @return \ArrayIterator
123
     */
124
    public function getIterator()
125
    {
126
        return new \ArrayIterator($this->wallpapers);
127
    }
128
129
    /**
130
     * @return int Wallpaper count.
131
     */
132
    public function count()
133
    {
134
        return count($this->wallpapers);
135
    }
136
137
    /**
138
     * All all items from the given list to the current list.
139
     *
140
     * @param WallpaperList $wallpaperList
141
     */
142
    public function addAll(WallpaperList $wallpaperList)
143
    {
144
        foreach ($wallpaperList as $wallpaper) {
145
            $this->wallpapers[] = $wallpaper;
146
        }
147
    }
148
}
149