Passed
Push — master ( ccabb0...25c117 )
by Peter
02:15
created

File::addCustomAssets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Http\Controllers\Admin\Form;
6
7
use AbterPhp\Admin\Http\Controllers\Admin\FormAbstract;
8
use AbterPhp\Framework\Assets\AssetManager;
9
use AbterPhp\Files\Domain\Entities\File as Entity;
10
use AbterPhp\Files\Domain\Entities\FileCategory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Files\Http\Cont...Admin\Form\FileCategory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use AbterPhp\Files\Form\Factory\File as FormFactory;
12
use AbterPhp\Files\Orm\FileRepo as Repo;
13
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
14
use AbterPhp\Framework\I18n\ITranslator;
15
use AbterPhp\Framework\Session\FlashService;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Routing\Urls\UrlGenerator;
18
use Opulence\Sessions\ISession;
19
use Psr\Log\LoggerInterface;
20
21
class File extends FormAbstract
22
{
23
    const ENTITY_PLURAL   = 'files';
24
    const ENTITY_SINGULAR = 'file';
25
26
    const ENTITY_TITLE_SINGULAR = 'files:file';
27
    const ENTITY_TITLE_PLURAL   = 'files:files';
28
29
    const ROUTING_PATH = 'files';
30
31
    /** @var string */
32
    protected $resource = 'files';
33
34
    /**
35
     * File constructor.
36
     *
37
     * @param FlashService     $flashService
38
     * @param ITranslator      $translator
39
     * @param UrlGenerator     $urlGenerator
40
     * @param LoggerInterface  $logger
41
     * @param Repo             $repo
42
     * @param ISession         $session
43
     * @param FormFactory      $formFactory
44
     * @param IEventDispatcher $eventDispatcher
45
     * @param AssetManager     $assetManager
46
     */
47
    public function __construct(
48
        FlashService $flashService,
49
        ITranslator $translator,
50
        UrlGenerator $urlGenerator,
51
        LoggerInterface $logger,
52
        Repo $repo,
53
        ISession $session,
54
        FormFactory $formFactory,
55
        IEventDispatcher $eventDispatcher,
56
        AssetManager $assetManager
57
    ) {
58
        parent::__construct(
59
            $flashService,
60
            $translator,
61
            $urlGenerator,
62
            $logger,
63
            $repo,
64
            $session,
65
            $formFactory,
66
            $eventDispatcher
67
        );
68
69
        $this->assetManager = $assetManager;
0 ignored issues
show
Bug Best Practice introduced by
The property assetManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
    }
71
72
    /**
73
     * @param string $entityId
74
     *
75
     * @return Entity
76
     */
77
    protected function createEntity(string $entityId): IStringerEntity
78
    {
79
        $fileCategory = new FileCategory('', '', '', false, []);
80
81
        return new Entity($entityId, '', '', '', '', $fileCategory, null);
82
    }
83
84
    /**
85
     * @param IStringerEntity|null $entity
86
     *
87
     * @throws \League\Flysystem\FileNotFoundException
88
     */
89
    protected function addCustomAssets(?IStringerEntity $entity = null)
90
    {
91
        parent::addCustomAssets($entity);
92
93
        if (!($entity instanceof Entity)) {
94
            return;
95
        }
96
97
        $footer = $this->getResourceName(static::RESOURCE_FOOTER);
98
        $this->assetManager->addJs($footer, '/admin-assets/js/semi-auto.js');
99
    }
100
}
101