Completed
Pull Request — master (#157)
by
unknown
01:35
created

LinkDrupal8::processValue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 1
1
<?php
2
namespace Drupal\Driver\Plugin\DriverField;
3
4
use Drupal\Driver\Plugin\DriverFieldPluginDrupal8Base;
5
6
/**
7
 * A driver field plugin for link fields.
8
 *
9
 * @DriverField(
10
 *   id = "link",
11
 *   version = 8,
12
 *   fieldTypes = {
13
 *     "link",
14
 *   },
15
 *   weight = -100,
16
 * )
17
 */
18
class LinkDrupal8 extends DriverFieldPluginDrupal8Base
19
{
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
    protected function assignPropertyNames($value)
25
    {
26
        // For links we support unkeyed arrays in which the first item is the title,
27
        // the second is the uri and third is options.
28
        $keyedValue = $value;
29
        if (!is_array($value)) {
30
            $keyedValue = ['uri' => $value];
31
        } elseif (count($value) === 1) {
32
            $keyedValue = ['uri' => end($value)];
33
        } // Convert unkeyed array.
34
        else {
35 View Code Duplication
            if (!isset($value['uri']) && isset($value[1])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
                $keyedValue['uri'] = $value[1];
37
                unset($keyedValue[1]);
38
            }
39 View Code Duplication
            if (!isset($value['title']) && isset($value[0])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                $keyedValue['title'] = $value[0];
41
                unset($keyedValue[0]);
42
            }
43 View Code Duplication
            if (!isset($value['options']) && isset($value[2])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
                $keyedValue['options'] = $value[2];
45
                unset($keyedValue[2]);
46
            }
47
        }
48
        if (!isset($keyedValue['uri'])) {
49
            throw new \Exception("Uri could not be identified from passed value: " . print_r($value, true));
50
        }
51
        return $keyedValue;
52
    }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
    protected function processValue($value)
58
    {
59
        // 'options' is required to be an array, otherwise the utility class
60
        // Drupal\Core\Utility\UnroutedUrlAssembler::assemble() will complain.
61
        $options = [];
62
        if (!empty($value['options'])) {
63
            parse_str($value['options'], $options);
64
        }
65
66
        // Default title to uri.
67
        $title = $value['uri'];
68
        if (isset($value['title'])) {
69
            $title = $value['title'];
70
        }
71
72
        $processedValue = [
73
        'uri' => $value['uri'],
74
        'title' => $title,
75
        'options' => $options,
76
        ];
77
        return $processedValue;
78
    }
79
}
80