AdminPaymentMethodsTrait::deleteFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 20
ccs 0
cts 14
cp 0
rs 9.7666
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace ByTIC\Payments\Controllers\Traits;
4
5
use ByTIC\Payments\Models\Methods\Traits\RecordsTrait;
6
use ByTIC\Payments\Models\Methods\Traits\RecordTrait;
7
use Nip\Records\AbstractModels\Record;
8
use Nip\Records\AbstractModels\RecordManager;
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Class AdminPaymentMethodsTrait
13
 * @package ByTIC\Payments\Payments\Controllers\Traits
14
 */
15
trait AdminPaymentMethodsTrait
16
{
17
    public function deleteFile()
18
    {
19
        $item = $this->getModelFromRequest();
20
        $fileName = $this->getRequest()->get('file');
21
22
        $type = 'error';
23
        $message = $this->getModelManager()->getMessage('deleteFile.error');
0 ignored issues
show
Unused Code introduced by
The assignment to $message is dead and can be removed.
Loading history...
24
        if ($fileName) {
25
            $files = $item->getFiles();
26
            if ($files[$fileName]) {
27
                $files[$fileName]->delete();
28
                $type = 'success';
29
                $message = $this->getModelManager()->getMessage('deleteFile.success');
30
            } else {
31
                $message = $this->getModelManager()->getMessage('deleteFile.no-file');
32
            }
33
        } else {
34
            $message = $this->getModelManager()->getMessage('deleteFile.no-filename');
35
        }
36
        $this->flashRedirect($message, $item->compileURL('view'), $type);
0 ignored issues
show
Bug introduced by
It seems like compileURL() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        $this->flashRedirect($message, $item->/** @scrutinizer ignore-call */ compileURL('view'), $type);
Loading history...
37
    }
38
39
    /**
40
     * @param bool $key
41
     * @return Record|RecordTrait
42
     */
43
    abstract protected function getModelFromRequest($key = false);
44
45
    /**
46
     * @return Request
47
     */
48
    abstract protected function getRequest();
49
50
    /**
51
     * @return RecordManager|RecordsTrait
52
     */
53
    abstract protected function getModelManager();
54
55
    /**
56
     * @param $message
57
     * @param $url
58
     * @param string $type
59
     * @param bool $name
60
     * @return mixed
61
     */
62
    abstract protected function flashRedirect($message, $url, $type = 'success', $name = false);
63
64
    public function delete()
65
    {
66
        $item = $this->getModelFromRequest();
67
68
        $deleteMessage = $item->canDelete();
69
        if ($deleteMessage !== true) {
70
            $url = $item->getURL();
0 ignored issues
show
Bug introduced by
It seems like getURL() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

70
            /** @scrutinizer ignore-call */ 
71
            $url = $item->getURL();
Loading history...
71
            $this->_flashName = $this->getModelManager()->getController();
0 ignored issues
show
Bug Best Practice introduced by
The property _flashName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
            $this->flashRedirect($deleteMessage, $url, 'error', $this->_flashName);
0 ignored issues
show
Bug introduced by
$this->_flashName of type string is incompatible with the type boolean expected by parameter $name of ByTIC\Payments\Controlle...sTrait::flashRedirect(). ( Ignorable by Annotation )

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

72
            $this->flashRedirect($deleteMessage, $url, 'error', /** @scrutinizer ignore-type */ $this->_flashName);
Loading history...
73
        }
74
75
        parent::delete();
76
    }
77
}
78