BarcodeController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace LearnZF2Barcode\Controller;
20
21
use Zend\Barcode\Barcode;
22
use Zend\Form\FormInterface;
23
use Zend\Mvc\Controller\AbstractActionController;
24
use Zend\View\Model\ViewModel;
25
26
class BarcodeController extends AbstractActionController
27
{
28
    /**
29
     * @var FormInterface
30
     */
31
    protected $form;
32
33
    /**
34
     * Construct the form.
35
     */
36
    public function __construct(FormInterface $form)
37
    {
38
        $this->form = $form;
39
    }
40
41
    /**
42
     * Use the form to get barcode image from selected barcode object.
43
     */
44
    public function indexAction()
45
    {
46
        $request = $this->getRequest();
47
48
        //default value without post parameter
49
        $barcodeOptions = ['text' => '123456789'];
50
        $barcode = Barcode::factory('codabar', 'image', $barcodeOptions);
51
52
        if ($request->isPost()) {
53
            $this->form->setData($request->getPost());
54
            if ($this->form->isValid()) {
55
                $barcodeOptions = ['text' => $this->form->getData()['barcode-object-text']];
56
                $barcode = Barcode::factory($this->form->getData()['barcode-object-select'], 'image', $barcodeOptions);
57
            }
58
        }
59
60
        imagegif($barcode->draw(), './data/barcode.gif');
61
62
        return new ViewModel([
63
            'form' => $this->form,
64
        ]);
65
    }
66
67
    /**
68
     * Show image barcode.
69
     */
70
    public function getbarcodeimageAction()
71
    {
72
        $response = $this->getResponse();
73
74
        if (file_exists('./data/barcode.gif')) {
75
            $response->getHeaders()->addHeaderLine('Content-Type', 'image/gif');
76
            $response->getHeaders()->addHeaderLine('Content-Length', filesize('./data/barcode.gif'));
77
            // set response with get content of gif
78
            $response->setContent(file_get_contents('./data/barcode.gif'));
79
80
            //remove file after no need
81
            @unlink('./data/barcode.gif');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
82
        } else {
83
            $response->getHeaders()->addHeaderLine('Content-Type', 'text/html');
84
            $response->setContent('barcode not found');
85
        }
86
87
        return $response;
88
    }
89
}
90