Code Duplication    Length = 53-54 lines in 2 locations

src/Validation/JsonApi/Rules/AreReadableViaApiRule.php 1 location

@@ 30-83 (lines=54) @@
27
/**
28
 * @package Limoncello\Flute
29
 */
30
final class AreReadableViaApiRule extends ExecuteRule
31
{
32
    /**
33
     * Property key.
34
     */
35
    const PROPERTY_API_CLASS = self::PROPERTY_LAST + 1;
36
37
    /**
38
     * @param string $apiClass
39
     */
40
    public function __construct(string $apiClass)
41
    {
42
        assert(
43
            class_exists($apiClass) === true &&
44
            array_key_exists(CrudInterface::class, class_implements($apiClass)) === true
45
        );
46
47
        parent::__construct([
48
            static::PROPERTY_API_CLASS => $apiClass,
49
        ]);
50
    }
51
52
    /**
53
     * @param mixed            $values
54
     * @param ContextInterface $context
55
     *
56
     * @return array
57
     *
58
     * @SuppressWarnings(PHPMD.StaticAccess)
59
     *
60
     * @throws ContainerExceptionInterface
61
     * @throws NotFoundExceptionInterface
62
     */
63
    public static function execute($values, ContextInterface $context): array
64
    {
65
        assert(is_array($values));
66
67
        $apiClass = $context->getProperties()->getProperty(static::PROPERTY_API_CLASS);
68
69
        /** @var FactoryInterface $apiFactory */
70
        $apiFactory = $context->getContainer()->get(FactoryInterface::class);
71
72
        /** @var CrudInterface $api */
73
        $api = $apiFactory->createApi($apiClass);
74
75
        $readIndexes = $api->withIndexesFilter($values)->indexIdentities();
76
77
        $result = count($readIndexes) === count($values);
78
79
        return $result === true ?
80
            static::createSuccessReply($values) :
81
            static::createErrorReply($context, $values, ErrorCodes::EXIST_IN_DATABASE_MULTIPLE);
82
    }
83
}
84

src/Validation/JsonApi/Rules/IsReadableViaApiRule.php 1 location

@@ 30-82 (lines=53) @@
27
/**
28
 * @package Limoncello\Flute
29
 */
30
final class IsReadableViaApiRule extends ExecuteRule
31
{
32
    /**
33
     * Property key.
34
     */
35
    const PROPERTY_API_CLASS = self::PROPERTY_LAST + 1;
36
37
    /**
38
     * @param string $apiClass
39
     */
40
    public function __construct(string $apiClass)
41
    {
42
        assert(
43
            class_exists($apiClass) === true &&
44
            array_key_exists(CrudInterface::class, class_implements($apiClass)) === true
45
        );
46
47
        parent::__construct([
48
            static::PROPERTY_API_CLASS => $apiClass,
49
        ]);
50
    }
51
52
    /**
53
     * @param mixed            $value
54
     * @param ContextInterface $context
55
     *
56
     * @return array
57
     *
58
     * @SuppressWarnings(PHPMD.StaticAccess)
59
     *
60
     * @throws ContainerExceptionInterface
61
     * @throws NotFoundExceptionInterface
62
     */
63
    public static function execute($value, ContextInterface $context): array
64
    {
65
        assert(is_int($value) || is_string($value));
66
67
        $apiClass = $context->getProperties()->getProperty(static::PROPERTY_API_CLASS);
68
69
        /** @var FactoryInterface $apiFactory */
70
        $apiFactory = $context->getContainer()->get(FactoryInterface::class);
71
72
        /** @var CrudInterface $api */
73
        $api = $apiFactory->createApi($apiClass);
74
75
        $data   = $api->withIndexFilter($value)->indexIdentities();
76
        $result = !empty($data);
77
78
        return $result === true ?
79
            static::createSuccessReply($value) :
80
            static::createErrorReply($context, $value, ErrorCodes::EXIST_IN_DATABASE_SINGLE);
81
    }
82
}
83