Code Duplication    Length = 47-47 lines in 2 locations

eZ/Publish/Core/Repository/RoleService.php 2 locations

@@ 346-392 (lines=47) @@
343
     *
344
     * @return \eZ\Publish\API\Repository\Values\User\RoleDraft
345
     */
346
    public function addPolicyByRoleDraft(APIRoleDraft $roleDraft, APIPolicyCreateStruct $policyCreateStruct)
347
    {
348
        if (!is_string($policyCreateStruct->module) || empty($policyCreateStruct->module)) {
349
            throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct');
350
        }
351
352
        if (!is_string($policyCreateStruct->function) || empty($policyCreateStruct->function)) {
353
            throw new InvalidArgumentValue('function', $policyCreateStruct->function, 'PolicyCreateStruct');
354
        }
355
356
        if ($policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*') {
357
            throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct');
358
        }
359
360
        if ($this->repository->hasAccess('role', 'update') !== true) {
361
            throw new UnauthorizedException('role', 'update');
362
        }
363
364
        $loadedRoleDraft = $this->loadRoleDraft($roleDraft->id);
365
366
        $limitations = $policyCreateStruct->getLimitations();
367
        $limitationValidationErrors = $this->validatePolicy(
368
            $policyCreateStruct->module,
369
            $policyCreateStruct->function,
370
            $limitations
371
        );
372
        if (!empty($limitationValidationErrors)) {
373
            throw new LimitationValidationException($limitationValidationErrors);
374
        }
375
376
        $spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject(
377
            $policyCreateStruct->module,
378
            $policyCreateStruct->function,
379
            $limitations
380
        );
381
382
        $this->repository->beginTransaction();
383
        try {
384
            $this->userHandler->addPolicyByRoleDraft($loadedRoleDraft->id, $spiPolicy);
385
            $this->repository->commit();
386
        } catch (Exception $e) {
387
            $this->repository->rollback();
388
            throw $e;
389
        }
390
391
        return $this->loadRoleDraft($loadedRoleDraft->id);
392
    }
393
394
    /**
395
     * Removes a policy from a RoleDraft.
@@ 630-676 (lines=47) @@
627
     *
628
     * @return \eZ\Publish\API\Repository\Values\User\Role
629
     */
630
    public function addPolicy(APIRole $role, APIPolicyCreateStruct $policyCreateStruct)
631
    {
632
        if (!is_string($policyCreateStruct->module) || empty($policyCreateStruct->module)) {
633
            throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct');
634
        }
635
636
        if (!is_string($policyCreateStruct->function) || empty($policyCreateStruct->function)) {
637
            throw new InvalidArgumentValue('function', $policyCreateStruct->function, 'PolicyCreateStruct');
638
        }
639
640
        if ($policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*') {
641
            throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct');
642
        }
643
644
        if ($this->repository->hasAccess('role', 'update') !== true) {
645
            throw new UnauthorizedException('role', 'update');
646
        }
647
648
        $loadedRole = $this->loadRole($role->id);
649
650
        $limitations = $policyCreateStruct->getLimitations();
651
        $limitationValidationErrors = $this->validatePolicy(
652
            $policyCreateStruct->module,
653
            $policyCreateStruct->function,
654
            $limitations
655
        );
656
        if (!empty($limitationValidationErrors)) {
657
            throw new LimitationValidationException($limitationValidationErrors);
658
        }
659
660
        $spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject(
661
            $policyCreateStruct->module,
662
            $policyCreateStruct->function,
663
            $limitations
664
        );
665
666
        $this->repository->beginTransaction();
667
        try {
668
            $this->userHandler->addPolicy($loadedRole->id, $spiPolicy);
669
            $this->repository->commit();
670
        } catch (Exception $e) {
671
            $this->repository->rollback();
672
            throw $e;
673
        }
674
675
        return $this->loadRole($loadedRole->id);
676
    }
677
678
    /**
679
     * Removes a policy from the role.