Importer::importContent method is very monolithic, inhibiting the class's extensibility.

For example, I'm building my own Content Manager service to import a single content item.
In order to do so, I have to override the entire method to change the logic only very slightly.

Decomposing importContent() into smaller methods will improve extensibility and make testing and maintenance easier.

Comments

aaronbauman created an issue. See original summary.

aaronbauman’s picture

Status: Active » Needs review
StatusFileSize
new8.38 KB

In this patch:
- create smaller helper methods to make importContent less monolithic
- no changes to logic or any other functionality

andypost’s picture

That's looks great, but I'd prefer to split the manager first

+++ b/src/DefaultContentManager.php
@@ -234,6 +177,120 @@ class DefaultContentManager implements DefaultContentManagerInterface {
+  ¶
...
+  }    ¶

nits, trailing whitespaces

Status: Needs review » Needs work
aaronbauman’s picture

Status: Needs work » Needs review

Ah, thank you i will follow up on 2614644 and pick those nits.

aaronbauman’s picture

Status: Needs review » Needs work
aaronbauman’s picture

Status: Needs work » Postponed

I'll pick this up again after we close 2614644

andypost’s picture

Title: Decompose DefaultContentManager::importContent method » Decompose Importer::importContent() method
Status: Postponed » Needs work
Issue tags: +Needs reroll, +Needs issue summary update

There's now single \Drupal\default_content\ImporterInterface::importContent()

aaronbauman’s picture

Assigned: Unassigned » aaronbauman
aaronbauman’s picture

Status: Needs work » Needs review
StatusFileSize
new8.01 KB

Rerolled @ importer

aaronbauman’s picture

Issue summary: View changes

Status: Needs review » Needs work
aaronbauman’s picture

Status: Needs work » Needs review
StatusFileSize
new8.01 KB
new536 bytes

s/entityManager/entityTypeManager/

Status: Needs review » Needs work
aaronbauman’s picture

Status: Needs work » Needs review
StatusFileSize
new8.54 KB
new3.1 KB

s/entity type id/entity type/

Status: Needs review » Needs work
aaronbauman’s picture

Status: Needs work » Needs review
StatusFileSize
new8.53 KB
new993 bytes

s/continue/return/

oy

Status: Needs review » Needs work
aaronbauman’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll, -Needs issue summary update
StatusFileSize
new8.54 KB

and again

Status: Needs review » Needs work
alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new10.29 KB

I like the way this is going. Here's some other thoughts:

  • I think we can get rid of the some of the static properties too so we don't need a reset.
  • I think the importContent should be at the top - its the main business of the class
  • I think we can do an early return in importContent if there is no folder
  • Exceptions shouldn't be using a markup object - sprintf is good.
  • The whole reflection dance to work out what's config / content entities is not required.

Here's an interdiff of the changes I would apply to #19

This also passes tests - you can run them locally there's only 2!

alexpott’s picture

@aaronbauman - fyi you can just apply the interdiff in #21 on top of your patch in #19 to see how it looks locally.

aaronbauman’s picture

Status: Needs review » Needs work

Thanks for the feedback.
I'll give this a go before i submit another.

This also passes tests - you can run them locally there's only 2!

Simpletest is killing me (and my machine).
I'll give it a shot.
Sorry for the WOB

alexpott’s picture

+++ b/src/Importer.php
@@ -112,6 +120,122 @@ class Importer implements ImporterInterface {
+    $files = $this->scanner()->scan($folder . '/' . $entity_type->id());
+    // Default content uses drupal.org as domain.
+    // @todo Make this use a uri like default-content:.
+    $this->linkManager->setLinkDomain(static::LINK_DOMAIN);

Both of these are fixed in the interdiff on #21. Should be $this->scanner->scan() and $this->linkManager->setLinkDomain($this->linkDomain).

@aaronbauman you can just run the default_content group. It's easiest from the command line. Something like:

sudo -u USER_RUNNING_APACHE php ./core/scripts/run-tests.sh --color --non-html --url http://YOU_LOCAL_URL/ --verbose default_content

You need to replace USER_RUNNING_APACHE and http://YOU_LOCAL_URL/ with whatever is right for you.

aaronbauman’s picture

Status: Needs work » Needs review
StatusFileSize
new10.29 KB
new11.66 KB

OK, this is passing locally.

Status: Needs review » Needs work
aaronbauman’s picture

Tests pass but testrunner segfaulted.
What now?

antonnavi’s picture

Status: Needs work » Needs review
aaronbauman’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
aaronbauman’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new11.59 KB

Rerolled @ latest dev
Passes locally

#25 is no longer applying because of interim changes, so no interdiff.

alexpott’s picture

  1. +++ b/src/Importer.php
    @@ -118,85 +115,157 @@ class Importer implements ImporterInterface {
    +      // There's no default to import.
    

    There's no default content to import.

  2. +++ b/src/Importer.php
    @@ -118,85 +115,157 @@ class Importer implements ImporterInterface {
    -      $this->eventDispatcher->dispatch(DefaultContentEvents::IMPORT, new ImportEvent($created, $module));
    -      $this->accountSwitcher->switchBack();
    

    This is a really good improvement since the event listeners definitely should not be invoked with the account we switch to.

larowlan’s picture

Looking good, thanks

andypost’s picture

Status: Needs review » Reviewed & tested by the community

#31 could be fixed on commit

berdir’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Reviewed & tested by the community » Needs work

I've recently created the 2.0.x branch, see the project page on all the improvements in the 2.0.x branch. The 1.x branch isn't actively maintained and won't receive new features anymore.

This will need a non-trivial reroll, there haven't been too many changes in the importer, but support for yaml/json was added and some things have been renamed.