Completed
Push — master ( 8bfd95...2626ba )
by Dan Michael O.
12:11
created

GoogleBooksService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 11 4
B enrich() 0 29 5
1
<?php
2
3
namespace Colligator;
4
5
use Scriptotek\GoogleBooks\GoogleBooks;
6
use Scriptotek\GoogleBooks\Exceptions\UsageRateExceeded;
7
8
class GoogleBooksService implements EnrichmentService
9
{
10
    public static $serviceName = 'googlebooks';
11
    protected $books;
12
13
    public function __construct(GoogleBooks $books)
14
    {
15
        $this->books = $books;
16
    }
17
18
    protected function get($isbns)
19
    {
20
        foreach ($isbns as $isbn) {
21
            $volume = $this->books->volumes->byIsbn($isbn);
22
            \Log::debug('[GoogleBooksService] - ' . $isbn . ': ' . (is_null($volume) ? 'not found' : 'found'));
23
            if (!is_null($volume)) {
24
                return $volume;
25
            }
26
        }
27
        return null;
28
    }
29
30
    public function enrich(Document $doc)
31
    {
32
        \Log::debug('[GoogleBooksService] Checking document ' . $doc->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Colligator\Document>. 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...
33
34
        $volume = $this->get($doc->bibliographic['isbns']);
0 ignored issues
show
Documentation introduced by
The property bibliographic does not exist on object<Colligator\Document>. 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...
35
36
        // Delete old
37
        $doc->enrichmentsByService(self::$serviceName)
38
            ->delete();
39
40
        // Insert
41
        $doc->enrichments()->create([
42
            'document_version' => sha1(json_encode($doc->bibliographic)),
0 ignored issues
show
Documentation introduced by
The property bibliographic does not exist on object<Colligator\Document>. 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
            'service_name' => self::$serviceName,
44
            'service_data' => $volume,
45
        ]);
46
47
        if ($volume) {
48
            $cover_url = $volume->getCover();
49
            if (!is_null($cover_url) && is_null($doc->cover)) {
0 ignored issues
show
Documentation introduced by
The property cover does not exist on object<Colligator\Document>. 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...
50
                \Log::debug('[GoogleBooksService] Setting new cover to document #' . $doc->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Colligator\Document>. 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...
51
                try {
52
                    $doc->storeCover($cover_url);
53
                } catch (\ErrorException $e) {
54
                    \Log::error('[GoogleBooksService] Failed to store cover: ' . $cover_url . "\n" . $e->getMessage());
55
                }
56
            }
57
        }
58
    }
59
}
60