Passed
Push — master ( 3de7ee...b534e3 )
by Mihail
14:25
created

ActionWallDelete   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
D wallDelete() 0 37 9
1
<?php
2
3
namespace Apps\Controller\Front\Profile;
4
5
6
use Apps\ActiveRecord\WallPost;
7
use Apps\Model\Front\Profile\FormWallPostDelete;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\ForbiddenException;
11
use Ffcms\Core\Exception\NotFoundException;
12
use Ffcms\Core\Helper\Type\Any;
13
use Ffcms\Core\Network\Response;
14
15
/**
16
 * Trait ActionWallDelete
17
 * @package Apps\Controller\Front\Profile
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionWallDelete
22
{
23
    /**
24
     * Allow post owners and targets delete
25
     * @param int $postId
26
     * @return string
27
     * @throws \Ffcms\Core\Exception\SyntaxException
28
     * @throws ForbiddenException
29
     * @throws NotFoundException
30
     */
31
    public function wallDelete($postId)
32
    {
33
        // user is auth?
34
        if (!App::$User->isAuth()) {
35
            throw new ForbiddenException();
36
        }
37
38
        // is postId is integer?
39
        if (!Any::isInt($postId) || $postId < 1) {
40
            throw new NotFoundException();
41
        }
42
43
        // try to find the wall post
44
        /** @var WallPost $wallPost */
45
        $wallPost = WallPost::find($postId);
46
        if (!$wallPost) {
47
            throw new NotFoundException();
48
        }
49
50
        // get user and check if he can delete this post
51
        $user = App::$User->identity();
52
        if ($wallPost->sender_id !== $user->id && $wallPost->target_id !== $user->id) {
53
            throw new ForbiddenException();
54
        }
55
56
        // check if submit sended
57
        $wallModel = new FormWallPostDelete($wallPost);
58
        if ($wallModel->send() && $wallModel->validate()) {
59
            $wallModel->make();
60
            $this->response->redirect('profile/show/' . $wallPost->target_id);
61
        }
62
63
        return $this->view->render('wall_delete', [
64
            'post' => $wallPost,
65
            'model' => $wallModel
66
        ]);
67
    }
68
69
}