Completed
Pull Request — develop (#251)
by
unknown
14:19 queued 06:05
created

AttachmentsFormFactory::configureForm()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 20
nc 3
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Cv\Form;
11
12
use Core\Form\FileUploadFactory;
13
use Core\Form\Form;
14
use Zend\Stdlib\AbstractOptions;
15
use Cv\Options\ModuleOptions;
16
17
/**
18
 * @author fedys
19
 */
20
class AttachmentsFormFactory extends FileUploadFactory
21
{
22
    /**
23
     * Form element for the file upload
24
     *
25
     * @var string
26
     */
27
    protected $fileElement = 'Core/FileUpload';
28
29
    /**
30
     * Name of the file, if downloaded.
31
     *
32
     * @var string
33
     */
34
    protected $fileName = 'attachments';
35
36
    /**
37
     * Entity for storing the attachment
38
     *
39
     * @var string
40
     */
41
    protected $fileEntityClass = '\Cv\Entity\Attachment';
42
43
    /**
44
     * allow to upload multiple files
45
     *
46
     * @var bool
47
     */
48
    protected $multiple = true;
49
50
    /**
51
     * use abstract options defined in "Cv/Options"
52
     *
53
     * @var string
54
     */
55
    protected $options="Cv/Options";
56
57
58
    /**
59
     * configure the formular for uploading attachments
60
     *
61
     * @param Form $form
62
     * @param AbstractOptions $options
63
     */
64
    protected function configureForm($form, AbstractOptions $options)
65
    {
66
        if (!$options instanceof ModuleOptions)
67
        {
68
            throw new \InvalidArgumentException(sprintf('$options must be instance of %s', ModuleOptions::class));
69
        }
70
        
71
        $size = $options->getAttachmentsMaxSize();
72
        $type = $options->getAttachmentsMimeType();
73
        $count = $options->getAttachmentsCount();
74
75
        $form->setIsDisableCapable(false)
76
             ->setIsDisableElementsCapable(false)
77
             ->setIsDescriptionsEnabled(true)
78
             ->setDescription(
79
                /*@translate*/ 'Attach images or PDF Documents to your CV. Drag&drop them, or click into the attachement area. You can upload up to %sMB',
80
                 [round($size/(1024*1024))>0? round($size/(1024*1024)):round($size/(1024*1024), 1)]
81
             )
82
             ->setParam('return', 'file-uri')
83
             ->setLabel(/*@translate*/ 'Attachments');
84
85
        /* @var $file \Core\Form\Element\FileUpload */
86
        $file = $form->get($this->fileName);
87
88
        $file->setMaxSize($size);
89
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
90
            $file->setAllowedTypes($type);
91
        }
92
        $file->setMaxFileCount($count);
93
94
        // pass form to element. Needed for file count validation
95
        // I did not find another (better) way.
96
        $file->setForm($form);
97
    }
98
}
99