WebResponses   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 24
c 4
b 1
f 0
dl 0
loc 59
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 5 1
A __construct() 0 4 1
A update() 0 5 1
A store() 0 4 1
A create() 0 5 1
A edit() 0 4 1
A index() 0 8 1
A show() 0 6 1
1
<?php
2
3
namespace Shamaseen\Repository\Utility;
4
5
use Illuminate\Contracts\Pagination\Paginator;
6
use Illuminate\Database\Eloquent\Collection;
7
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...
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Support\Facades\Config;
10
use Illuminate\Support\Facades\Redirect;
11
use Illuminate\Support\Facades\View;
12
use Shamaseen\Repository\Interfaces\CrudResponse;
13
14
class WebResponses implements CrudResponse
15
{
16
    private Controller $controller;
17
18
    public function __construct(Controller $controller)
19
    {
20
        $this->controller = $controller;
21
        View::share('breadcrumbs', $controller->breadcrumbs);
22
    }
23
24
    public function index(Paginator|Collection $paginate): \Illuminate\Contracts\View\View
25
    {
26
        View::share('pageTitle', __('repository.list').' '.$this->controller->pageTitle.' | '.Config::get('app.name'));
27
28
        return view($this->controller->viewIndex, $this->controller->params)
29
            ->with('entities', $paginate)
30
            ->with('createRoute', $this->controller->createRoute)
31
            ->with('filters', $this->controller->request->all());
32
    }
33
34
    public function show(Model $entity): \Illuminate\Contracts\View\View
35
    {
36
        View::share('pageTitle', 'View '.$this->controller->pageTitle.' | '.Config::get('app.name'));
37
38
        return view($this->controller->viewShow, $this->controller->params)
39
            ->with('entity', $entity);
40
    }
41
42
    public function create(): \Illuminate\Contracts\View\View
43
    {
44
        View::share('pageTitle', 'Create '.$this->controller->pageTitle.' | '.Config::get('app.name'));
45
46
        return view($this->controller->viewCreate, $this->controller->params);
47
    }
48
49
    public function store(Model $entity): RedirectResponse
50
    {
51
        return Redirect::to($this->controller->resolveRoute($this->controller->routeIndex))
52
            ->with('message', __('repository.created_successfully'));
53
    }
54
55
    public function edit(Model $entity): \Illuminate\Contracts\View\View
56
    {
57
        return view($this->controller->viewEdit, $this->controller->params)
58
            ->with('entity', $entity);
59
    }
60
61
    public function update(int|bool|Model $updatedCount): RedirectResponse
62
    {
63
        return Redirect::to($this->controller->resolveRoute($this->controller->routeIndex))
64
            ->with('message', __('repository.modified_successfully'))
65
            ->with('updatedCount', $updatedCount);
66
    }
67
68
    public function destroy(int|bool $destroyedCount): RedirectResponse
69
    {
70
        return Redirect::to($this->controller->resolveRoute($this->controller->routeIndex))
71
            ->with('message', __('repository.deleted_successfully'))
72
            ->with('destroyedCount', $destroyedCount);
73
    }
74
}
75