Completed
Push — master ( 6c5b86...657a58 )
by Dan Michael O.
01:30
created

Volume   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 82
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D getCover() 0 27 9
A removeCoverEdge() 0 7 2
A __get() 0 8 3
A __isset() 0 4 2
A __toString() 0 4 1
1
<?php
2
3
namespace Scriptotek\GoogleBooks;
4
5
class Volume
6
{
7
    protected $client;
8
    protected $data;
9
    protected $full = false;
10
11
    protected static $coverSizes = [
12
        "extraLarge",
13
        "large",
14
        "medium",
15
        "small",
16
        "thumbnail",
17
        "smallThumbnail",
18
    ];
19
20
    public function __construct($client, $data, $full=false)
0 ignored issues
show
Unused Code introduced by
The parameter $full 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...
21
    {
22
        $this->client = $client;
23
        $this->data = $data;
24
    }
25
26
    /**
27
     * Returns cover of size $preferredSize or smaller if a cover of size $preferredSize does not exist.
28
     *
29
     * @param string $preferredSize
30
     * @return mixed|null
31
     */
32
    public function getCover($preferredSize='extraLarge')
33
    {
34
        $url = null;
35
36
        if (!isset($this->data->volumeInfo) || !isset($this->data->volumeInfo->imageLinks)) {
37
            return null;
38
        }
39
40
        if (!$this->full && !in_array($preferredSize, ['thumbnail', 'smallThumbnail'])) {
41
            // Need to fetch the full record
42
            $this->data = $this->client->getItem('volumes/' . $this->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Scriptotek\GoogleBooks\Volume>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
        }
44
45
        $idx = array_search($preferredSize, self::$coverSizes);
46
        if ($idx === false) {
47
            throw new \InvalidArgumentException('Invalid size: ' . $preferredSize);
48
        }
49
50
        foreach (self::$coverSizes as $n => $size) {
51
            if ($n >= $idx && isset($this->data->volumeInfo->imageLinks->{$size})) {
52
                $url = $this->data->volumeInfo->imageLinks->{$size};
53
                break;
54
            }
55
        }
56
57
        return $this->removeCoverEdge($url);
58
    }
59
60
    protected function removeCoverEdge($url) {
61
        // Remove cover edge
62
        if (is_null($url)) {
63
            return null;
64
        }
65
        return str_replace('&edge=curl', '&edge=none', $url);
66
    }
67
68
    public function __get($key)
69
    {
70
        if (isset($this->data->volumeInfo->{$key})) {
71
            return $this->data->volumeInfo->{$key};
72
        } else if (isset($this->data->{$key})) {
73
            return $this->data->{$key};
74
        }
75
    }
76
    
77
    public function __isset($key)
78
    {
79
        return isset($this->data->volumeInfo->{$key}) || isset($this->data->{$key});
80
    }
81
82
    public function __toString()
83
    {
84
        return json_encode($this->data);
85
    }
86
}
87