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]); |
|
|
|
|
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]); |
|
|
|
|
85
|
|
|
default: |
86
|
|
|
throw new RuntimeException('Unable to detect the output'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: