ComposerJsonValidator::validate()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of `Composer as a service`.
5
 *
6
 * (c) Pascal Borreli <[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
namespace Ayaline\Bundle\ComposerBundle\Validator\Constraints;
13
14
use Composer\Json\JsonFile;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\ConstraintValidator;
17
18
/**
19
 * @author Abdellatif Ait boudad <[email protected]>
20
 */
21
class ComposerJsonValidator extends ConstraintValidator
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function validate($value, Constraint $constraint)
27
    {
28
        $tempFile = tempnam(sys_get_temp_dir(), 'composer');
29
        file_put_contents($tempFile, $value);
30
31
        try {
32
            $jsonFile = new JsonFile($tempFile);
33
            $jsonFile->validateSchema(JsonFile::LAX_SCHEMA);
34
            unlink($tempFile);
35
        } catch (\Exception $exception) {
36
            unlink($tempFile);
37
            $from = [$tempFile];
38
            $to = ['composer.json'];
39
            $this->context->addViolation(str_replace($from, $to, $exception->getMessage()));
40
        }
41
    }
42
}
43