RichEditorConstraints   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 19
c 2
b 0
f 0
dl 0
loc 64
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getVideoConstraints() 0 20 4
A getImageConstraints() 0 17 4
1
<?php
2
3
/*
4
 * This file is part of Monsieur Biz' Rich Editor plugin for Sylius.
5
 *
6
 * (c) Monsieur Biz <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace MonsieurBiz\SyliusRichEditorPlugin\Form\Constraints;
15
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
final class RichEditorConstraints
19
{
20
    /**
21
     * Return constraint depending on data
22
     * If user created the element, the field is required
23
     * If it's an edition and it contains a filename, we don't flag it as required.
24
     *
25
     * @param array $data
26
     * @param string $fieldName
27
     * @param bool $required
28
     *
29
     * @return array
30
     */
31
    public static function getImageConstraints(array $data, string $fieldName, bool $required = true)
32
    {
33
        // No constraint if current value is a string with the filepath
34
        if (isset($data[$fieldName]) && \is_string($data[$fieldName])) {
35
            return [];
36
        }
37
38
        if (!$required) {
39
            return [
40
                new Assert\Image([]),
41
            ];
42
        }
43
44
        // No image set yet, we require file
45
        return [
46
            new Assert\NotBlank([]),
47
            new Assert\Image([]),
48
        ];
49
    }
50
51
    /**
52
     * Return constraint depending on data
53
     * If user created the element, the field is required
54
     * If it's an edition and it contains a filename, we don't flag it as required.
55
     *
56
     * @param array $data
57
     * @param string $fieldName
58
     * @param bool $required
59
     *
60
     * @return array
61
     */
62
    public static function getVideoConstraints(array $data, string $fieldName, bool $required = true)
63
    {
64
        // No constraint if current value is a string with the filepath
65
        if (isset($data[$fieldName]) && \is_string($data[$fieldName])) {
66
            return [];
67
        }
68
69
        if (!$required) {
70
            return [
71
                new Assert\File([
72
                    'mimeTypes' => ['video/mpeg', 'video/mp4', 'video/quicktime', 'video/x-ms-wmv', 'video/x-msvideo', 'video/x-flv'],
73
                ]),
74
            ];
75
        }
76
77
        // No video set yet, we require file
78
        return [
79
            new Assert\NotBlank([]),
80
            new Assert\File([
81
                'mimeTypes' => ['video/mpeg', 'video/mp4', 'video/quicktime', 'video/x-ms-wmv', 'video/x-msvideo', 'video/x-flv'],
82
            ]),
83
        ];
84
    }
85
}
86