Completed
Push — develop ( 728aa6...c30dd4 )
by
unknown
17:43 queued 10:24
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
 * @since 0.26
20
 */
21
class AttachmentsFormFactory extends FileUploadFactory
22
{
23
    /**
24
     * Form element for the file upload
25
     *
26
     * @var string
27
     */
28
    protected $fileElement = 'Core/FileUpload';
29
30
    /**
31
     * Name of the file, if downloaded.
32
     *
33
     * @var string
34
     */
35
    protected $fileName = 'attachments';
36
37
    /**
38
     * Entity for storing the attachment
39
     *
40
     * @var string
41
     */
42
    protected $fileEntityClass = '\Cv\Entity\Attachment';
43
44
    /**
45
     * allow to upload multiple files
46
     *
47
     * @var bool
48
     */
49
    protected $multiple = true;
50
51
    /**
52
     * use abstract options defined in "Cv/Options"
53
     *
54
     * @var string
55
     */
56
    protected $options="Cv/Options";
57
58
59
    /**
60
     * configure the formular for uploading attachments
61
     *
62
     * @param Form $form
63
     * @param AbstractOptions $options
64
     */
65
    protected function configureForm($form, AbstractOptions $options)
66
    {
67
        if (!$options instanceof ModuleOptions)
68
        {
69
            throw new \InvalidArgumentException(sprintf('$options must be instance of %s', ModuleOptions::class));
70
        }
71
        
72
        $size = $options->getAttachmentsMaxSize();
73
        $type = $options->getAttachmentsMimeType();
74
        $count = $options->getAttachmentsCount();
75
76
        $form->setIsDisableCapable(false)
77
             ->setIsDisableElementsCapable(false)
78
             ->setIsDescriptionsEnabled(true)
79
             ->setDescription(
80
                /*@translate*/ 'Attach images or PDF Documents to your CV. Drag&drop them, or click into the attachement area. You can upload up to %sMB',
81
                 [round($size/(1024*1024))>0? round($size/(1024*1024)):round($size/(1024*1024), 1)]
82
             )
83
             ->setParam('return', 'file-uri')
84
             ->setLabel(/*@translate*/ 'Attachments');
85
86
        /* @var $file \Core\Form\Element\FileUpload */
87
        $file = $form->get($this->fileName);
88
89
        $file->setMaxSize($size);
90
        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...
91
            $file->setAllowedTypes($type);
92
        }
93
        $file->setMaxFileCount($count);
94
95
        // pass form to element. Needed for file count validation
96
        // I did not find another (better) way.
97
        $file->setForm($form);
98
    }
99
}
100