Completed
Push — develop ( b4fd40...785f8f )
by greg
03:10
created

ShortenUrl::setServiceManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace PlaygroundCore\Service;
3
4
use Zend\ServiceManager\ServiceManager;
5
use ZfcBase\EventManager\EventProvider;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
/**
9
 * main class
10
 */
11
class ShortenUrl extends EventProvider
12
{
13
    /**
14
     * @var ModuleOptions
15
     */
16
    protected $options;
17
18
    /**
19
     *
20
     * @var ServiceManager
21
     */
22
    protected $serviceLocator;
23
24
    public function __construct(ServiceLocatorInterface $locator)
25
    {
26
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
27
    }
28
29
    /**
30
     * This method call Bit.ly to shorten a given URL.
31
     * @param  unknown_type $url
32
     * @return unknown
33
     */
34
    public function shortenUrl($url)
35
    {
36
        if ($this->getOptions()->getBitlyApiKey() && $this->getOptions()->getBitlyUsername()) {
37
            $client = new \Zend\Http\Client($this->getOptions()->getBitlyUrl());
38
            $client->setParameterGet(array(
39
                'format'  => 'json',
40
                'longUrl' => $url,
41
                'login'   => $this->getOptions()->getBitlyUsername(),
42
                'apiKey'  => $this->getOptions()->getBitlyApiKey(),
43
            ));
44
    
45
            $result = $client->send();
46
            if ($result) {
47
                $jsonResult = \Zend\Json\Json::decode($result->getBody());
48
                if ($jsonResult->status_code == 200) {
49
                    return $jsonResult->data->url;
50
                }
51
            }
52
        }
53
54
        return $url;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $url; (PlaygroundCore\Service\unknown_type) is incompatible with the return type documented by PlaygroundCore\Service\ShortenUrl::shortenUrl of type PlaygroundCore\Service\unknown.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
55
    }
56
57
    public function setOptions($options)
58
    {
59
        $this->options = $options;
60
61
        return $this;
62
    }
63
64
    public function getOptions()
65
    {
66
        if (!$this->options) {
67
            $this->setOptions($this->serviceLocator->get('playgroundcore_module_options'));
68
        }
69
70
        return $this->options;
71
    }
72
}
73