Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
09:08
created

ContactsController::user()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
nc 4
nop 1
dl 0
loc 26
rs 9.1111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\Controller;
14
15
use App\Form\ContactForm;
16
use App\Form\ContactFormOwner;
17
use Cake\Datasource\Exception\RecordNotFoundException;
18
use Cake\Event\Event;
19
use Cake\Form\Form;
20
use Cake\Http\Exception\BadRequestException;
21
use Cake\ORM\TableRegistry;
22
use Saito\Exception\Logger\ExceptionLogger;
23
24
class ContactsController extends AppController
25
{
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    public function beforeFilter(Event $event)
31
    {
32
        parent::beforeFilter($event);
33
        $this->set('showDisclaimer', true);
34
        $this->Authentication->allowUnauthenticated(['owner']);
35
    }
36
37
    /**
38
     * Contacts forum's owner via contact address
39
     *
40
     * @return void
41
     */
42
    public function owner()
43
    {
44
        $recipient = 'contact';
45
        if ($this->CurrentUser->isLoggedIn()) {
46
            $user = $this->CurrentUser;
47
            $sender = $user->getId();
48
            $this->request = $this->request->withData('sender_contact', $user->get('user_email'));
49
        } else {
50
            $senderContact = $this->request->getData('sender_contact');
51
            $sender = [$senderContact => $senderContact];
52
        }
53
54
        $this->_contact(new ContactFormOwner(), $recipient, $sender);
55
    }
56
57
    /**
58
     * Contacts individual user
59
     *
60
     * @param string $id user-ID
61
     * @return void
62
     * @throws \InvalidArgumentException
63
     * @throws BadRequestException
64
     */
65
    public function user($id = null)
66
    {
67
        if (empty($id) || !$this->CurrentUser->isLoggedIn()) {
68
            throw new BadRequestException();
69
        }
70
71
        $Users = TableRegistry::get('Users');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        $Users = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Users');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
72
        try {
73
            $recipient = $Users->get($id);
74
        } catch (RecordNotFoundException $e) {
75
            throw new BadRequestException();
76
        }
77
        $this->set('user', $recipient);
78
79
        if (!$recipient->get('personal_messages')
80
            && !$this->CurrentUser->permission('saito.core.user.contact')) {
81
            throw new BadRequestException(null, 1562415010);
82
        }
83
84
        $this->set(
85
            'titleForPage',
86
            __('user_contact_title', $recipient->get('username'))
87
        );
88
89
        $sender = $this->CurrentUser->getId();
90
        $this->_contact(new ContactForm(), $recipient, $sender);
91
    }
92
93
    /**
94
     *  contact form validating and email sending
95
     *
96
     * @param Form $contact contact-form
97
     * @param mixed $recipient recipient
98
     * @param mixed $sender sender
99
     * @return \Cake\Network\Response|void
100
     */
101
    protected function _contact(Form $contact, $recipient, $sender)
102
    {
103
        if ($this->request->is('get')) {
104
            if ($this->request->getData('cc') === null) {
105
                $this->request = $this->request->withData('cc', true);
106
            }
107
        }
108
109
        if ($this->request->is('post')) {
110
            $isValid = $contact->validate($this->request->getData());
0 ignored issues
show
Bug introduced by
It seems like $this->request->getData() can also be of type null; however, parameter $data of Cake\Form\Form::validate() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            $isValid = $contact->validate(/** @scrutinizer ignore-type */ $this->request->getData());
Loading history...
111
            if ($isValid) {
112
                try {
113
                    $email = [
114
                        'recipient' => $recipient,
115
                        'sender' => $sender,
116
                        'subject' => $this->request->getData('subject'),
117
                        'message' => $this->request->getData('text'),
118
                        'template' => 'user_contact',
119
                        'ccsender' => (bool)$this->request->getData('cc'),
120
                    ];
121
                    $this->SaitoEmail->email($email);
122
                    $message = __('Message was send.');
123
                    $this->Flash->set($message, ['element' => 'success']);
124
125
                    return $this->redirect('/');
126
                } catch (\Exception $e) {
127
                    $Logger = new ExceptionLogger();
128
                    $Logger->write('Contact email failed', ['e' => $e]);
129
                    $message = $e->getMessage();
130
                    $message = __('Message couldn\'t be send: {0}', $message);
131
                    $this->Flash->set($message, ['element' => 'error']);
132
                }
133
            }
134
        }
135
136
        $this->set(compact('contact'));
137
    }
138
}
139