Passed
Push — master ( f9a48a...945dc8 )
by Mike
03:01
created

AbstractGetFileEntryPoint::getDownloadDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * ©[2016] SugarCRM Inc.  Licensed by SugarCRM under the Apache 2.0 license.
4
 */
5
6
namespace SugarAPI\SDK\EntryPoint\Abstracts\GET;
7
8
use SugarAPI\SDK\EntryPoint\Abstracts\AbstractEntryPoint;
9
use SugarAPI\SDK\Request\GETFile;
10
use SugarAPI\SDK\Response\File as FileResponse;
11
12
abstract class AbstractGetFileEntryPoint extends AbstractEntryPoint {
13
14
    /**
15
     * The directory in which to download the File
16
     * @var string
17
     */
18
    protected $downloadDir = null;
19
20 1
    public function __construct($url, array $options = array()){
21 1
        $this->setRequest(new GETFile());
22 1
        $this->setResponse(new FileResponse($this->Request->getCurlObject()));
23 1
        parent::__construct($url, $options);
24 1
    }
25
26 1
    public function configureResponse(){
27 1
        $this->Response->setDestinationPath($this->downloadDir);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SugarAPI\SDK\Response\Interfaces\ResponseInterface as the method setDestinationPath() does only exist in the following implementations of said interface: SugarAPI\SDK\Response\File.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
28 1
        parent::configureResponse();
29 1
    }
30
31
    /**
32
     * Set the download directory for the File the EntryPoint is retrieving
33
     * @param $path
34
     * @return $this
35
     */
36 1
    public function downloadTo($path){
37 1
        $this->downloadDir = $path;
38 1
        return $this;
39
    }
40
41
    /**
42
     * Get the download directory for the File the EntryPoint is retrieving
43
     * @return string
44
     */
45 1
    public function getDownloadDir(){
46 1
        return $this->downloadDir;
47
    }
48
49
}