Issues (6)

src/IndexPage/IndexPageManager.php (1 issue)

1
<?php
2
3
namespace UserAdmin\IndexPage;
4
5
use Illuminate\Contracts\Foundation\Application;
6
7
class IndexPageManager
8
{
9
    /**
10
     * The application instance.
11
     */
12
    protected Application $app;
13
14
    /**
15
     * Allowed pages list.
16
     * @var array|string[]
17
     */
18
    protected array $pages = [];
19
20
    /**
21
     * Resolved page names. Key is name, value is class name
22
     * @var array
23
     */
24
    protected array $resolvedArray = [];
25
26
    /**
27
     * Create a new instance.
28
     *
29
     * @param Application $app
30
     */
31 7
    public function __construct(Application $app)
32
    {
33 7
        $this->app = $app;
34 7
    }
35
36
    /**
37
     * @param array $pages
38
     *
39
     * @return array
40
     */
41 7
    public function usePages(array $pages = [])
42
    {
43 7
        return $this->pages = $pages;
44
    }
45
46
    /**
47
     * @return string[]
48
     */
49 6
    public function pages(): array
50
    {
51 6
        return $this->pages;
52
    }
53
54
    /**
55
     * Resolve page class.
56
     *
57
     * @param string $name
58
     *
59
     * @return mixed
60
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
61
     */
62 6
    public function resolve(string $name)
63
    {
64 6
        return $this->app->make($this->getPageByName($name));
65
    }
66
67
    /**
68
     * Get page class name.
69
     *
70
     * @param string $name
71
     *
72
     * @return string
73
     * @throws \Exception
74
     */
75 6
    public function getPageByName(string $name): string
76
    {
77 6
        if (!$this->resolvedArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->resolvedArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78 6
            $this->resolveArray();
79
        }
80 6
        if (!isset($this->resolvedArray[ $name ])) {
81 2
            throw new \Exception("Index page [{$name}] not exists");
82
        }
83
84 6
        return $this->resolvedArray[ $name ];
85
    }
86
87
    /**
88
     * Resolve all page names
89
     */
90 6
    protected function resolveArray(): static
91
    {
92 6
        $resolvedArray = [];
93
94 6
        foreach ($this->pages() as $page) {
95 6
            if (is_string($page) && is_subclass_of($page, IndexPage::class)) {
96 6
                $resolvedArray[ $page::name() ] = $page;
97
            }
98
        }
99
100 6
        $this->resolvedArray = $resolvedArray;
101
102 6
        return $this;
103
    }
104
}
105