From da3d4998c0972557952c83b610f8f45fdcd31b72 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 28 Sep 2015 19:35:33 +0200 Subject: Move readingTime & domainName in ContentProxy So, everything is centralized in one place when we save a new entry. --- src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php | 12 ++++++++++++ src/Wallabag/CoreBundle/Entity/Entry.php | 3 --- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 3 +++ src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php | 14 ++++++++++---- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index dd316194..b4b685f9 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -17,6 +17,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface { $entry1 = new Entry($this->getReference('admin-user')); $entry1->setUrl('http://0.0.0.0'); + $entry1->setReadingTime(11); + $entry1->setDomainName('domain.io'); $entry1->setTitle('test title entry1'); $entry1->setContent('This is my content /o/'); $entry1->setLanguage('en'); @@ -27,6 +29,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry2 = new Entry($this->getReference('admin-user')); $entry2->setUrl('http://0.0.0.0'); + $entry2->setReadingTime(1); + $entry2->setDomainName('domain.io'); $entry2->setTitle('test title entry2'); $entry2->setContent('This is my content /o/'); $entry2->setLanguage('fr'); @@ -37,6 +41,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry3 = new Entry($this->getReference('bob-user')); $entry3->setUrl('http://0.0.0.0'); + $entry3->setReadingTime(1); + $entry3->setDomainName('domain.io'); $entry3->setTitle('test title entry3'); $entry3->setContent('This is my content /o/'); $entry3->setLanguage('en'); @@ -55,6 +61,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry4 = new Entry($this->getReference('admin-user')); $entry4->setUrl('http://0.0.0.0'); + $entry4->setReadingTime(12); + $entry4->setDomainName('domain.io'); $entry4->setTitle('test title entry4'); $entry4->setContent('This is my content /o/'); $entry4->setLanguage('en'); @@ -73,6 +81,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry5 = new Entry($this->getReference('admin-user')); $entry5->setUrl('http://0.0.0.0'); + $entry5->setReadingTime(12); + $entry5->setDomainName('domain.io'); $entry5->setTitle('test title entry5'); $entry5->setContent('This is my content /o/'); $entry5->setStarred(true); @@ -84,6 +94,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry6 = new Entry($this->getReference('admin-user')); $entry6->setUrl('http://0.0.0.0'); + $entry6->setReadingTime(12); + $entry6->setDomainName('domain.io'); $entry6->setTitle('test title entry6'); $entry6->setContent('This is my content /o/'); $entry6->setArchived(true); diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 7108889e..9e81ba12 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -7,7 +7,6 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; use JMS\Serializer\Annotation\XmlRoot; -use Wallabag\CoreBundle\Tools\Utils; /** * Entry. @@ -279,8 +278,6 @@ class Entry public function setContent($content) { $this->content = $content; - $this->readingTime = Utils::getReadingTime($content); - $this->domainName = parse_url($this->url, PHP_URL_HOST); return $this; } diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index 3de8828f..7fb41393 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\Helper; use Graby\Graby; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\CoreBundle\Tools\Utils; /** * This kind of proxy class take care of getting the content from an url @@ -51,6 +52,8 @@ class ContentProxy $entry->setContent($html); $entry->setLanguage($content['language']); $entry->setMimetype($content['content_type']); + $entry->setReadingTime(Utils::getReadingTime($html)); + $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST)); if (isset($content['open_graph']['og_image'])) { $entry->setPreviewPicture($content['open_graph']['og_image']); diff --git a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php index 30065d6b..0d338389 100644 --- a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php +++ b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php @@ -35,6 +35,8 @@ class ContentProxyTest extends KernelTestCase $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getMimetype()); $this->assertEmpty($entry->getLanguage()); + $this->assertEquals(0.0, $entry->getReadingTime()); + $this->assertEquals('0.0.0.0', $entry->getDomainName()); } public function testWithEmptyContentButOG() @@ -59,14 +61,16 @@ class ContentProxyTest extends KernelTestCase )); $proxy = new ContentProxy($graby); - $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); + $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); - $this->assertEquals('http://0.0.0.0', $entry->getUrl()); + $this->assertEquals('http://domain.io', $entry->getUrl()); $this->assertEquals('my title', $entry->getTitle()); $this->assertEquals('

Unable to retrieve readable content.

But we found a short description:

desc', $entry->getContent()); $this->assertEmpty($entry->getPreviewPicture()); $this->assertEmpty($entry->getLanguage()); $this->assertEmpty($entry->getMimetype()); + $this->assertEquals(0.0, $entry->getReadingTime()); + $this->assertEquals('domain.io', $entry->getDomainName()); } public function testWithContent() @@ -79,7 +83,7 @@ class ContentProxyTest extends KernelTestCase $graby->expects($this->any()) ->method('fetchContent') ->willReturn(array( - 'html' => 'this is my content', + 'html' => str_repeat('this is my content', 325), 'title' => 'this is my title', 'url' => 'http://1.1.1.1', 'content_type' => 'text/html', @@ -96,9 +100,11 @@ class ContentProxyTest extends KernelTestCase $this->assertEquals('http://1.1.1.1', $entry->getUrl()); $this->assertEquals('this is my title', $entry->getTitle()); - $this->assertEquals('this is my content', $entry->getContent()); + $this->assertContains('this is my content', $entry->getContent()); $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); $this->assertEquals('text/html', $entry->getMimetype()); $this->assertEquals('fr', $entry->getLanguage()); + $this->assertEquals(4.0, $entry->getReadingTime()); + $this->assertEquals('1.1.1.1', $entry->getDomainName()); } } -- cgit v1.2.3