Completed
Pull Request — master (#64)
by Thibaud
03:06
created

Uploader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 50%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 70
ccs 16
cts 32
cp 0.5
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C upload() 0 39 8
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhraseanetSDK;
13
14
use PhraseanetSDK\Entity\DataboxCollection;
15
use PhraseanetSDK\Entity\Quarantine;
16
use PhraseanetSDK\Entity\Record;
17
use PhraseanetSDK\Exception\RuntimeException;
18
use PhraseanetSDK\Http\APIGuzzleAdapter;
19
20
class Uploader
21
{
22
    /** @var APIGuzzleAdapter */
23
    private $adapter;
24
25
    /** EntityManager */
26
    private $em;
27
28
    /**
29
     * @param APIGuzzleAdapter $adapter
30
     * @param EntityManager $em
31
     */
32 7
    public function __construct(APIGuzzleAdapter $adapter, EntityManager $em)
33
    {
34 7
        $this->adapter = $adapter;
35 7
        $this->em = $em;
36 7
    }
37
38
    /**
39
     * Uploads a file to Phraseanet.
40
     *
41
     * @param string $file The path to the file to upload
42
     * @param integer|DataboxCollection $collection The base_id of the collection or a DataboxCollection object
43
     * @param int|null $behavior Set to 0 to force record and bypass checks, Set to 1 to force quarantine.
44
     * @param int|null $status A binary string to set status bits.
45
     *
46
     * @return Record|Quarantine
47
     *
48
     * @throws RuntimeException In case an error occurred
49
     */
50 6
    public function upload($file, $collection, $behavior = null, $status = null)
51
    {
52
        $postFields = array(
53 6
            'base_id' => $collection instanceof DataboxCollection ? $collection->getBaseId() : $collection,
54 6
        );
55
56 6
        if (null !== $behavior) {
57 2
            $postFields['forceBehavior'] = $behavior;
58 2
        }
59
60 6
        if (null !== $status) {
61 5
            $postFields['status'] = $status;
62 5
        }
63
64 6
        $response = $this->adapter->call('POST', 'v1/records/add/', array(), $postFields, array(
65 6
            'file' => $file,
66 6
        ));
67
68
        switch ((int)$response->getResult()->entity) {
69
            case 0:
70
                $matches = array();
71
                preg_match('/\/records\/(\d+)\/(\d+)\//', $response->getResult()->url, $matches);
72
                if (3 !== count($matches)) {
73
                    throw new RuntimeException('Unable to find the record item back');
74
                }
75
76
                return $this->em->getRepository('record')->findById($matches[1], $matches[2]);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhraseanetSDK\AbstractRepository as the method findById() does only exist in the following sub-classes of PhraseanetSDK\AbstractRepository: PhraseanetSDK\Repository\Entry, PhraseanetSDK\Repository\Feed, PhraseanetSDK\Repository\Quarantine, PhraseanetSDK\Repository\Record, PhraseanetSDK\Repository\Story. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
77
            case 1:
78
                $matches = array();
79
                preg_match('/quarantine\/item\/(\d+)\//', $response->getResult()->url, $matches);
80
                if (2 !== count($matches)) {
81
                    throw new RuntimeException('Unable to find the quarantine item back');
82
                }
83
84
                return $this->em->getRepository('quarantine')->findById($matches[1]);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhraseanetSDK\AbstractRepository as the method findById() does only exist in the following sub-classes of PhraseanetSDK\AbstractRepository: PhraseanetSDK\Repository\Entry, PhraseanetSDK\Repository\Feed, PhraseanetSDK\Repository\Quarantine, PhraseanetSDK\Repository\Record, PhraseanetSDK\Repository\Story. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
85
            default:
86
                throw new RuntimeException('Unable to detect the output');
87
        }
88
    }
89
}
90