ResponseDispatcher::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Shamaseen\Repository\Utility;
4
5
use Illuminate\Contracts\Pagination\Paginator;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Shamaseen\Repository\Utility\Model. 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...
9
use Illuminate\Http\JsonResponse;
10
use Illuminate\Http\RedirectResponse;
11
use Shamaseen\Repository\Interfaces\CrudResponse;
12
13
class ResponseDispatcher implements CrudResponse
14
{
15
    private WebResponses|APIResponses $dispatcher;
16
17
    public function __construct(Controller $controller)
18
    {
19
        if ('api' === config('repository.responses')) {
20
            $this->dispatcher = (new APIResponses($controller));
21
        } elseif ('web' === config('repository.responses')) {
22
            $this->dispatcher = (new WebResponses($controller));
23
        } elseif ($controller->isAPI) {
24
            $this->dispatcher = (new APIResponses($controller));
25
        } else {
26
            $this->dispatcher = (new WebResponses($controller));
27
        }
28
    }
29
30
    public function index(Paginator|Collection $paginate): View|JsonResponse
31
    {
32
        return $this->dispatcher->index($paginate);
33
    }
34
35
    public function show(Model $entity): View|JsonResponse
36
    {
37
        return $this->dispatcher->show($entity);
38
    }
39
40
    public function create(): View|JsonResponse
41
    {
42
        return $this->dispatcher->create();
43
    }
44
45
    public function store(Model $entity): JsonResponse|RedirectResponse
46
    {
47
        return $this->dispatcher->store($entity);
48
    }
49
50
    public function edit(Model $entity): View|JsonResponse
51
    {
52
        return $this->dispatcher->edit($entity);
53
    }
54
55
    public function update(int|bool|Model $updatedCount): JsonResponse|RedirectResponse
56
    {
57
        return $this->dispatcher->update($updatedCount);
58
    }
59
60
    public function destroy(int|bool $destroyedCount): JsonResponse|RedirectResponse
61
    {
62
        return $this->dispatcher->destroy($destroyedCount);
63
    }
64
}
65