Completed
Pull Request — master (#1)
by Spencer
02:07
created

GetTicketStatusResponse::fromCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
namespace FloSports\ScaleEngine\Response;
3
4
use FloSports\ScaleEngine\Model\Factory\ScaleEngineTicketFactory;
5
use Guzzle\Service\Command\OperationCommand;
6
7
/**
8
 * Provides the ability to convert a command response into a ScaleEngineTicket
9
 * model.
10
 */
11
class GetTicketStatusResponse extends AbstractScaleEngineResponse
12
{
13
    /**
14
     * Get the response from the command as a ScaleEngineTicket model.
15
     *
16
     * This takes the command given, ensures that it has a successful response,
17
     * and converts the response into a ScaleEngineTicket model.
18
     *
19
     * @param OperationCommand $command The GetTicketStatus API call made.
20
     * @param ScaleEngineTicketFactory $modelFactory The factory to use to
21
     *     create tickets.  Will be created if not given.
22
     * @return ScaleEngineTicket The ticket returned from the API request.
23
     */
24
    public static function fromCommand(OperationCommand $command, ScaleEngineTicketFactory $modelFactory = null)
25
    {
26
        $modelFactory = $modelFactory ?: new ScaleEngineTicketFactory();
27
28
        return $modelFactory->create(self::getResult($command));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $modelFactory->cr...::getResult($command)); (Guzzle\Service\Resource\Model) is incompatible with the return type declared by the interface Guzzle\Service\Command\R...sInterface::fromCommand of type Guzzle\Service\Command\ResponseClassInterface.

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...
29
    }
30
}
31