Read::allowed()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @package File manager
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\file_manager\handlers\commands;
11
12
/**
13
 * Contains methods for "read" command
14
 */
15
class Read extends Command
16
{
17
18
    /**
19
     * Whether the command is allowed for the file
20
     * @param \SplFileInfo $file
21
     * @return bool
22
     */
23
    public function allowed($file)
24
    {
25
        return $file->isFile() && $file->isReadable();
26
    }
27
28
    /**
29
     * Returns an array of data used to display the command
30
     * @param \SplFileInfo $file
31
     * @param \gplcart\core\Controller $controller
32
     * @return array
33
     */
34
    public function view($file, $controller)
35
    {
36
        return array(
37
            'file_manager|commands/read' => array(
38
                'content' => $this->callReadMethod($file, $controller)));
39
    }
40
41
    /**
42
     * Returns an array if reader methods and their supported file extensions
43
     * @return array
44
     */
45
    protected function getMethods()
46
    {
47
        return array(
48
            'viewText' => array('php', 'css', 'js', 'txt'),
49
            'viewImage' => array('jpg', 'jpeg', 'gif', 'png')
50
        );
51
    }
52
53
    /**
54
     * Calls a method to read a file using its extension
55
     * @param \SplFileInfo $file
56
     * @param \gplcart\core\Controller $controller
57
     * @return string
58
     */
59
    protected function callReadMethod($file, $controller)
60
    {
61
        if (!$file->isFile()) {
62
            return '';
63
        }
64
65
        $extension = $file->getExtension();
66
67
        foreach ($this->getMethods() as $method => $extensions) {
68
            if (in_array($extension, $extensions)) {
69
                return $this->$method($file, $controller);
70
            }
71
        }
72
73
        return $this->viewInfo($file, $controller);
74
    }
75
76
    /**
77
     * Returns rendered image content
78
     * @param \SplFileInfo $file
79
     * @param \gplcart\core\Controller $controller
80
     * @return string
81
     */
82
    protected function viewImage($file, $controller)
83
    {
84
        $path = $file->getRealPath();
85
86
        $data = array(
87
            'file' => $file,
88
            'src' => $controller->image(gplcart_file_relative($path)),
89
            'exif' => function_exists('exif_read_data') ? exif_read_data($path) : array()
90
        );
91
92
        return $controller->render('file_manager|readers/image', $data);
93
    }
94
95
    /**
96
     * Returns rendered text content
97
     * @param \SplFileInfo $file
98
     * @param \gplcart\core\Controller $controller
99
     * @return string
100
     */
101
    protected function viewText($file, $controller)
102
    {
103
        $data = array(
104
            'file' => $file,
105
            'content' => file_get_contents($file->getRealPath())
106
        );
107
108
        return $controller->render('file_manager|readers/text', $data);
109
    }
110
111
    /**
112
     * Returns rendered file info content
113
     * @param \SplFileInfo $file
114
     * @param \gplcart\core\Controller $controller
115
     * @return string
116
     */
117
    protected function viewInfo($file, $controller)
118
    {
119
        $data = array(
120
            'file' => $file,
121
            'perms' => gplcart_file_perms($file->getPerms()),
122
            'filesize' => gplcart_file_size($file->getSize())
123
        );
124
125
        return $controller->render('file_manager|readers/info', $data);
126
    }
127
128
}
129