Completed
Push — master ( f813e0...5f1c56 )
by Thierry
03:59 queued 02:07
created

Upload::saveUploadedFiles()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 10
nop 0
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Upload.php - Upload Trait
5
 *
6
 * The Jaxon class uses a modular plug-in system to facilitate the processing
7
 * of special Ajax requests made by a PHP page.
8
 * It generates Javascript that the page must include in order to make requests.
9
 * It handles the output of response commands (see <Jaxon\Response\Response>).
10
 * Many flags and settings can be adjusted to effect the behavior of the Jaxon class
11
 * as well as the client-side javascript.
12
 *
13
 * @package jaxon-core
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright 2017 Thierry Feuzeu <[email protected]>
16
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
17
 * @link https://github.com/jaxon-php/jaxon-core
18
 */
19
20
namespace Jaxon\Traits;
21
22
use Jaxon\Plugin\Manager as PluginManager;
23
use Jaxon\Request\Manager as RequestManager;
24
use Jaxon\Response\Manager as ResponseManager;
25
26
use Jaxon\Utils\URI;
27
use Exception;
28
use Closure;
29
30
trait Upload
31
{
32
   /**
33
     * Check if uploaded files are available
34
     *
35
     * @return boolean
36
     */
37
    public function hasUploadedFiles()
38
    {
39
        if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null)
0 ignored issues
show
Bug introduced by
It seems like getPluginManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
        {
41
            return false;
42
        }
43
        return $xUploadPlugin->canProcessRequest();
44
    }
45
46
   /**
47
     * Check uploaded files validity and move them to the user dir
48
     *
49
     * @return boolean
50
     */
51
    public function saveUploadedFiles()
52
    {
53
        try
54
        {
55
            if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null)
0 ignored issues
show
Bug introduced by
It seems like getPluginManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
            {
57
                throw new Exception($this->trans('errors.upload.plugin'));
0 ignored issues
show
Bug introduced by
It seems like trans() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58
            }
59
            elseif(!$xUploadPlugin->canProcessRequest())
60
            {
61
                throw new Exception($this->trans('errors.upload.request'));
0 ignored issues
show
Bug introduced by
It seems like trans() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
            }
63
            // Save uploaded files
64
            $sKey = $xUploadPlugin->saveUploadedFiles();
65
            $sResponse = '{"code": "success", "upl": "' . $sKey . '"}';
66
            $return = true;
67
        }
68
        catch(Exception $e)
69
        {
70
            $sResponse = '{"code": "error", "msg": "' . addslashes($e->getMessage()) . '"}';
71
            $return = false;
72
        }
73
        // Send the response back to the browser
74
        echo '<script>var res = ', $sResponse, '; </script>';
75
        if(($this->getOption('core.process.exit')))
0 ignored issues
show
Bug introduced by
It seems like getOption() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
        {
77
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method saveUploadedFiles() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
78
        }
79
        return $return;
80
    }
81
82
    /**
83
     * Get the uploaded files
84
     *
85
     * @return array
86
     */
87
    public function getUploadedFiles()
88
    {
89
        if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null)
0 ignored issues
show
Bug introduced by
It seems like getPluginManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
90
        {
91
            return [];
92
        }
93
        return $xUploadPlugin->getUploadedFiles();
94
    }
95
96
    /**
97
     * Filter uploaded file name
98
     *
99
     * @param Closure       $fFileFilter            The closure which filters filenames
100
     *
101
     * @return void
102
     */
103
    public function setUploadFileFilter(Closure $fFileFilter)
104
    {
105
        if(($xUploadPlugin = $this->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD)) == null)
0 ignored issues
show
Bug introduced by
It seems like getPluginManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
106
        {
107
            return;
108
        }
109
        $xUploadPlugin->setFileFilter($fFileFilter);
110
    }
111
}
112