Completed
Push — master ( 5b5792...2b84bd )
by Schlaefer
03:33 queued 11s
created

ContactsController::owner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
101
            if ($isValid) {
102
                try {
103
                    $email = [
104
                        'recipient' => $recipient,
105
                        'sender' => $sender,
106
                        'subject' => $this->request->getData('subject'),
107
                        'message' => $this->request->getData('text'),
108
                        'template' => 'user_contact',
109
                        'ccsender' => (bool)$this->request->getData('cc'),
110
                    ];
111
                    $this->SaitoEmail->email($email);
112
                    $message = __('Message was send.');
113
                    $this->Flash->set($message, ['element' => 'success']);
114
115
                    return $this->redirect('/');
116
                } catch (\Exception $e) {
117
                    $Logger = new ExceptionLogger();
118
                    $Logger->write('Contact email failed', ['e' => $e]);
119
                    $message = $e->getMessage();
120
                    $message = __('Message couldn\'t be send: {0}', $message);
121
                    $this->Flash->set($message, ['element' => 'error']);
122
                }
123
            }
124
        }
125
126
        $this->set(compact('contact'));
127
    }
128
}
129