File::addCustomAssets()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
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\Files\Domain\Entities\File as Entity;
9
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...
10
use AbterPhp\Files\Form\Factory\File as FormFactory;
11
use AbterPhp\Files\Orm\FileRepo as Repo;
12
use AbterPhp\Framework\Assets\AssetManager;
13
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
14
use AbterPhp\Framework\I18n\ITranslator;
15
use AbterPhp\Framework\Session\FlashService;
16
use League\Flysystem\FilesystemException;
17
use Opulence\Events\Dispatchers\IEventDispatcher;
18
use Opulence\Routing\Urls\UrlGenerator;
19
use Opulence\Sessions\ISession;
20
use Psr\Log\LoggerInterface;
21
22
class File extends FormAbstract
23
{
24
    const ENTITY_PLURAL   = 'files';
25
    const ENTITY_SINGULAR = 'file';
26
27
    const ENTITY_TITLE_SINGULAR = 'files:file';
28
    const ENTITY_TITLE_PLURAL   = 'files:files';
29
30
    const ROUTING_PATH = 'files';
31
32
    /** @var AssetManager */
33
    protected $assetManager;
34
35
    /** @var string */
36
    protected $resource = 'files';
37
38
    /**
39
     * File constructor.
40
     *
41
     * @param FlashService     $flashService
42
     * @param LoggerInterface  $logger
43
     * @param ITranslator      $translator
44
     * @param UrlGenerator     $urlGenerator
45
     * @param Repo             $repo
46
     * @param ISession         $session
47
     * @param FormFactory      $formFactory
48
     * @param IEventDispatcher $eventDispatcher
49
     * @param AssetManager     $assetManager
50
     */
51
    public function __construct(
52
        FlashService $flashService,
53
        LoggerInterface $logger,
54
        ITranslator $translator,
55
        UrlGenerator $urlGenerator,
56
        Repo $repo,
57
        ISession $session,
58
        FormFactory $formFactory,
59
        IEventDispatcher $eventDispatcher,
60
        AssetManager $assetManager
61
    ) {
62
        parent::__construct(
63
            $flashService,
64
            $logger,
65
            $translator,
66
            $urlGenerator,
67
            $repo,
68
            $session,
69
            $formFactory,
70
            $eventDispatcher
71
        );
72
73
        $this->assetManager = $assetManager;
74
    }
75
76
    /**
77
     * @param string $entityId
78
     *
79
     * @return Entity
80
     */
81
    protected function createEntity(string $entityId): IStringerEntity
82
    {
83
        $fileCategory = new FileCategory('', '', '', false, []);
84
85
        return new Entity($entityId, '', '', '', '', $fileCategory, null);
86
    }
87
88
    /**
89
     * @param IStringerEntity|null $entity
90
     *
91
     * @throws FilesystemException
92
     */
93
    protected function addCustomAssets(?IStringerEntity $entity = null)
94
    {
95
        parent::addCustomAssets($entity);
96
97
        if (!($entity instanceof Entity)) {
98
            return;
99
        }
100
101
        $footer = $this->getResourceName(static::RESOURCE_FOOTER);
102
        $this->assetManager->addJs($footer, '/admin-assets/js/semi-auto.js');
103
        $this->assetManager->addJs($footer, '/admin-assets/js/required.js');
104
        $this->assetManager->addJs($footer, '/admin-assets/js/validation.js');
105
    }
106
}
107