Completed
Push — develop ( fd6fdd...e2e6ec )
by Abdelrahman
01:24
created

ContactsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Contacts\Http\Controllers\Tenantarea;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Contacts\Contracts\ContactContract;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Contacts\DataTables\Tenantarea\ContactsDataTable;
11
use Cortex\Foundation\Http\Controllers\AuthorizedController;
12
use Cortex\Contacts\Http\Requests\Tenantarea\ContactFormRequest;
13
14
class ContactsController extends AuthorizedController
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $resource = 'contacts';
20
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function index()
27
    {
28
        return app(ContactsDataTable::class)->with([
29
            'id' => 'cortex-contacts',
30
            'phrase' => trans('cortex/contacts::common.contacts'),
31
        ])->render('cortex/foundation::tenantarea.pages.datatable');
32
    }
33
34
    /**
35
     * Display a listing of the resource logs.
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function logs(ContactContract $contact)
40
    {
41
        return app(LogsDataTable::class)->with([
42
            'type' => 'contacts',
43
            'resource' => $contact,
44
            'id' => 'cortex-contacts-logs',
45
            'phrase' => trans('cortex/contacts::common.contacts'),
46
        ])->render('cortex/foundation::tenantarea.pages.datatable-logs');
47
    }
48
49
    /**
50
     * Store a newly created resource in storage.
51
     *
52
     * @param \Cortex\Contacts\Http\Requests\Tenantarea\ContactFormRequest $request
53
     *
54
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56
    public function store(ContactFormRequest $request)
57
    {
58
        return $this->process($request, app('rinvex.contacts.contact'));
59
    }
60
61
    /**
62
     * Update the given resource in storage.
63
     *
64
     * @param \Cortex\Contacts\Http\Requests\Tenantarea\ContactFormRequest $request
65
     * @param \Rinvex\Contacts\Contracts\ContactContract                  $contact
66
     *
67
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    public function update(ContactFormRequest $request, ContactContract $contact)
70
    {
71
        return $this->process($request, $contact);
72
    }
73
74
    /**
75
     * Delete the given resource from storage.
76
     *
77
     * @param \Rinvex\Contacts\Contracts\ContactContract $contact
78
     *
79
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
80
     */
81
    public function delete(ContactContract $contact)
82
    {
83
        $contact->delete();
84
85
        return intend([
86
            'url' => route('tenantarea.contacts.index'),
87
            'with' => ['warning' => trans('cortex/contacts::messages.contact.deleted', ['slug' => $contact->slug])],
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Rinvex\Contacts\Contracts\ContactContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
88
        ]);
89
    }
90
91
    /**
92
     * Show the form for create/update of the given resource.
93
     *
94
     * @param \Rinvex\Contacts\Contracts\ContactContract $contact
95
     *
96
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
97
     */
98
    public function form(ContactContract $contact)
99
    {
100
        $countries = countries();
101
        $languages = collect(languages())->pluck('name', 'iso_639_1');
102
        $sources = app('rinvex.contacts.contact')->distinct()->get(['source'])->pluck('source', 'source')->toArray();
103
        $methods = app('rinvex.contacts.contact')->distinct()->get(['method'])->pluck('method', 'method')->toArray();
104
        $genders = ['m' => trans('cortex/contacts::common.male'), 'f' => trans('cortex/contacts::common.female')];
105
106
        return view('cortex/contacts::tenantarea.forms.contact', compact('contact', 'genders', 'countries', 'languages', 'sources', 'methods'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
107
    }
108
109
    /**
110
     * Process the form for store/update of the given resource.
111
     *
112
     * @param \Illuminate\Http\Request                   $request
113
     * @param \Rinvex\Contacts\Contracts\ContactContract $contact
114
     *
115
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
116
     */
117
    protected function process(Request $request, ContactContract $contact)
118
    {
119
        // Prepare required input fields
120
        $data = $request->all();
121
122
        // Save contact
123
        $contact->fill($data)->save();
124
        $contact->attachTenants(config('rinvex.tenants.tenant.active'));
0 ignored issues
show
Bug introduced by
The method attachTenants() does not seem to exist on object<Rinvex\Contacts\Contracts\ContactContract>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
126
        return intend([
127
            'url' => route('tenantarea.contacts.index'),
128
            'with' => ['success' => trans('cortex/contacts::messages.contact.saved', ['slug' => $contact->slug])],
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Rinvex\Contacts\Contracts\ContactContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
129
        ]);
130
    }
131
}
132