1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Service\Execute; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract; |
8
|
|
|
use AbterPhp\Admin\Domain\Entities\User; |
9
|
|
|
use AbterPhp\Admin\Domain\Entities\UserLanguage; |
10
|
|
|
use AbterPhp\Files\Domain\Entities\File; |
|
|
|
|
11
|
|
|
use AbterPhp\Files\Domain\Entities\FileDownload as Entity; |
12
|
|
|
use AbterPhp\Files\Orm\FileDownloadRepo as GridRepo; |
13
|
|
|
use AbterPhp\Files\Validation\Factory\FileDownload as ValidatorFactory; |
14
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
15
|
|
|
use Cocur\Slugify\Slugify; |
16
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
17
|
|
|
use Opulence\Http\Requests\UploadedFile; |
18
|
|
|
use Opulence\Orm\IUnitOfWork; |
19
|
|
|
|
20
|
|
|
class FileDownload extends RepoServiceAbstract |
21
|
|
|
{ |
22
|
|
|
/** @var Slugify */ |
23
|
|
|
protected $slugify; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* FileDownload constructor. |
27
|
|
|
* |
28
|
|
|
* @param GridRepo $repo |
29
|
|
|
* @param ValidatorFactory $validatorFactory |
30
|
|
|
* @param IUnitOfWork $unitOfWork |
31
|
|
|
* @param IEventDispatcher $eventDispatcher |
32
|
|
|
* @param Slugify $slugify |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
GridRepo $repo, |
36
|
|
|
ValidatorFactory $validatorFactory, |
37
|
|
|
IUnitOfWork $unitOfWork, |
38
|
|
|
IEventDispatcher $eventDispatcher, |
39
|
|
|
Slugify $slugify |
40
|
|
|
) { |
41
|
|
|
parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher); |
42
|
|
|
|
43
|
|
|
$this->slugify = $slugify; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $entityId |
48
|
|
|
* |
49
|
|
|
* @return Entity |
50
|
|
|
*/ |
51
|
|
|
public function createEntity(string $entityId): IStringerEntity |
52
|
|
|
{ |
53
|
|
|
$file = new File('', '', '', '', ''); |
54
|
|
|
$userLanguage = new UserLanguage('', '', ''); |
55
|
|
|
$user = new User('', '', '', '', false, false, $userLanguage); |
56
|
|
|
|
57
|
|
|
return new Entity($entityId, $file, $user, new \DateTime()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
62
|
|
|
* |
63
|
|
|
* @param IStringerEntity $entity |
64
|
|
|
* @param array $postData |
65
|
|
|
* @param UploadedFile[] $fileData |
66
|
|
|
* |
67
|
|
|
* @return Entity |
68
|
|
|
*/ |
69
|
|
|
protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity |
70
|
|
|
{ |
71
|
|
|
if (!($entity instanceof Entity)) { |
72
|
|
|
throw new \InvalidArgumentException('Invalid entity'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$file = $entity->getFile(); |
76
|
|
|
$file->setId($postData['file_id']); |
77
|
|
|
|
78
|
|
|
$user = $entity->getUser(); |
79
|
|
|
$user->setId($postData['user_id']); |
80
|
|
|
|
81
|
|
|
return $entity; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: