FileItemConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A convert() 0 18 4
A canConvert() 0 4 1
1
<?php
2
3
namespace Drupal\easy_entity_reader\Converters;
4
5
use Drupal\Core\Field\FieldItemInterface;
6
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
7
use Drupal\easy_entity_reader\EntityWrapper;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\file\Plugin\Field\FieldType\FileItem;
10
11
/**
12
 * Convert the file item class to value.
13
 *
14
 * Class FileItemConverter.
15
 */
16
class FileItemConverter implements ConverterInterface
17
{
18
    /**
19
     * @var EntityWrapper
20
     */
21
    private $entityWrapper;
22
23
    /**
24
     * @var EntityTypeManagerInterface
25
     */
26
    private $entityManager;
27
28
    /**
29
     * @param EntityWrapper              $entityWrapper
30
     * @param EntityTypeManagerInterface $entityManager
31
     */
32
    public function __construct(EntityWrapper $entityWrapper,
33
        EntityTypeManagerInterface $entityManager)
34
    {
35
        $this->entityWrapper = $entityWrapper;
36
        $this->entityManager = $entityManager;
37
    }
38
39
    /**
40
     * @param FieldItemInterface $value
41
     */
42
    public function convert(FieldItemInterface $value)
43
    {
44
        $node = null;
45
        $values = $value->getValue();
46
        $type = str_replace('default:', '', $value->getParent()->getSettings()['handler']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Core\TypedData\Tr...sableTypedDataInterface as the method getSettings() does only exist in the following implementations of said interface: Drupal\Core\Field\ChangedFieldItemList, Drupal\Core\Field\EntityReferenceFieldItemList, Drupal\Core\Field\FieldItemBase, Drupal\Core\Field\FieldItemList, Drupal\Core\Field\MapFieldItemList, Drupal\Core\Field\Plugin...d\FieldType\BooleanItem, Drupal\Core\Field\Plugin...d\FieldType\ChangedItem, Drupal\Core\Field\Plugin...d\FieldType\CreatedItem, Drupal\Core\Field\Plugin...d\FieldType\DecimalItem, Drupal\Core\Field\Plugin\Field\FieldType\EmailItem, Drupal\Core\Field\Plugin...ype\EntityReferenceItem, Drupal\Core\Field\Plugin\Field\FieldType\FloatItem, Drupal\Core\Field\Plugin...d\FieldType\IntegerItem, Drupal\Core\Field\Plugin...\FieldType\LanguageItem, Drupal\Core\Field\Plugin\Field\FieldType\MapItem, Drupal\Core\Field\Plugin...eldType\NumericItemBase, Drupal\Core\Field\Plugin...\FieldType\PasswordItem, Drupal\Core\Field\Plugin...ld\FieldType\StringItem, Drupal\Core\Field\Plugin...ieldType\StringItemBase, Drupal\Core\Field\Plugin...ieldType\StringLongItem, Drupal\Core\Field\Plugin...FieldType\TimestampItem, Drupal\Core\Field\Plugin\Field\FieldType\UriItem, Drupal\Core\Field\Plugin\Field\FieldType\UuidItem, Drupal\color_field\Plugi...ieldType\ColorFieldType, Drupal\comment\CommentFieldItemList, Drupal\comment\Plugin\Field\FieldType\CommentItem, Drupal\content_moderatio...ationStateFieldItemList, Drupal\datetime\Plugin\F...e\DateTimeFieldItemList, Drupal\datetime\Plugin\F...\FieldType\DateTimeItem, Drupal\datetime_range\Pl...\DateRangeFieldItemList, Drupal\datetime_range\Pl...FieldType\DateRangeItem, Drupal\entity_reference\...ableEntityReferenceItem, Drupal\entity_test\Plugi...mputedTestFieldItemList, Drupal\entity_test\Plugi...utoIncrementingTestItem, Drupal\entity_test\Plugi...eldType\ChangedTestItem, Drupal\entity_test\Plugi...FieldType\FieldTestItem, Drupal\entity_test\Plugi...alPropertyTestFieldItem, Drupal\entity_test\Plugi...eld\FieldType\ShapeItem, Drupal\entity_test\Plugi...dType\ShapeItemRequired, Drupal\entity_test_updat...Type\MultiValueTestItem, Drupal\field_collection\...eldType\FieldCollection, Drupal\field_test\Plugin...ieldType\HiddenTestItem, Drupal\field_test\Plugin\Field\FieldType\TestItem, Drupal\field_test\Plugin...estItemWithDependencies, Drupal\field_test\Plugin...ithPreconfiguredOptions, Drupal\field_test\Plugin...ieldType\TestObjectItem, Drupal\file\Plugin\Field...dType\FileFieldItemList, Drupal\file\Plugin\Field\FieldType\FileItem, Drupal\file\Plugin\Field\FieldType\FileUriItem, Drupal\image\Plugin\Field\FieldType\ImageItem, Drupal\language\DefaultLanguageItem, Drupal\layout_builder\Field\LayoutSectionItemList, Drupal\layout_builder\Pl...dType\LayoutSectionItem, Drupal\link\Plugin\Field\FieldType\LinkItem, Drupal\options\Plugin\Fi...FieldType\ListFloatItem, Drupal\options\Plugin\Fi...eldType\ListIntegerItem, Drupal\options\Plugin\Field\FieldType\ListItemBase, Drupal\options\Plugin\Fi...ieldType\ListStringItem, Drupal\path\Plugin\Field...dType\PathFieldItemList, Drupal\path\Plugin\Field\FieldType\PathItem, Drupal\telephone\Plugin\...FieldType\TelephoneItem, Drupal\text\Plugin\Field\FieldType\TextItem, Drupal\text\Plugin\Field\FieldType\TextItemBase, Drupal\text\Plugin\Field\FieldType\TextLongItem, Drupal\text\Plugin\Field...ype\TextWithSummaryItem, Drupal\user\StatusItem, Drupal\user\TimeZoneItem, Drupal\user\UserNameItem.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
47
        if(isset($values['target_id'])) {
48
            $node = $this->entityManager->getStorage($type)->load($values['target_id']);
49
        }
50
        elseif(isset($value->getValue()['fids'][0])) {
51
            $node = $this->entityManager->getStorage($type)->load($value->getValue()['fids'][0]);
52
        }
53
        if($node !== null) {
54
            return $this->entityWrapper->wrap($node);
55
        }
56
        else {
57
            return null;
58
        }
59
    }
60
61
    /**
62
     * Returns whether the FieldItemInterface can be converted or not.
63
     *
64
     * @param FieldItemInterface $value
65
     *
66
     * @return bool
67
     */
68
    public function canConvert(FieldItemInterface $value) : bool
69
    {
70
        return $value instanceof FileItem;
71
    }
72
}
73