Passed
Push — 8.x ( fadd51...4cf850 )
by Christopher
21:11 queued 01:56
created

ParagraphAccessCheck::access()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 3
dl 0
loc 20
ccs 0
cts 14
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\paragraphs_editor\Access;
4
5
use Drupal\Core\Access\AccessResult;
6
use Drupal\Core\Routing\Access\AccessInterface;
7
use Drupal\Core\Routing\RouteMatchInterface;
8
use Drupal\Core\Session\AccountInterface;
9
use Drupal\paragraphs_editor\EditBuffer\EditBufferItemFactoryInterface;
10
use Symfony\Component\Routing\Route;
11
12
/**
13
 * An access check handler for checking access to an editor paragraph.
14
 */
15
class ParagraphAccessCheck implements AccessInterface {
16
17
  protected $itemFactory;
18
19
  /**
20
   * The key used by the routing requirement.
21
   *
22
   * @var string
23
   */
24
  protected $requirementsKey = '_paragraphs_editor_access_paragraph';
25
26
  /**
27
   * Creates a paragraph access check object.
28
   *
29
   * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemFactoryInterface $item_factory
30
   *   The factory to use for looking up edit buffer items.
31
   */
32
  public function __construct(EditBufferItemFactoryInterface $item_factory) {
33
    $this->itemFactory = $item_factory;
34
  }
35
36
  /**
37
   * Determines if the user has access to edit a paragraph item.
38
   *
39
   * A user has access to edit a paragraph item if the paragraph item can be
40
   * located within the editor context and the user has access to the editor
41
   * context.
42
   *
43
   * @param \Symfony\Component\Routing\Route $route
44
   *   The route the user is attempting to access.
45
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
46
   *   The route match for the route the user is attempting to access.
47
   * @param \Drupal\Core\Session\AccountInterface $account
48
   *   The account to check access against.
49
   *
50
   * @return \Drupal\Core\Access\AccessResultInterface
51
   *   The access result for the user.
52
   */
53
  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
54
    list($context_param_name, $paragraph_param_name) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
55
56
    // Load the context from the parameters.
57
    $context = ContextAccessCheck::extractContext($route_match, $context_param_name);
58
    if (empty($context)) {
59
      return AccessResult::forbidden();
60
    }
61
62
    // Load the paragraph uuid from the parameters.
63
    if (preg_match('/\{(.*)\}$/', $paragraph_param_name, $matches)) {
64
      $paragraph_uuid = $route_match->getParameter($matches[1]);
65
    }
66
    else {
67
      $paragraph_uuid = $paragraph_param_name;
68
    }
69
70
    // If the paragraph item cannot be located we treat it as an access denied.
71
    $exists = !!$this->itemFactory->getBufferItem($context, $paragraph_uuid);
72
    return AccessResult::allowedIf($exists);
73
  }
74
75
}
76