Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented twig methods sulu_article_load_recent sulu_article_load_similar #193

Merged
13 changes: 9 additions & 4 deletions Content/ArticleDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class ArticleDataProvider implements DataProviderInterface, DataProviderAliasInt
*/
private $referenceStore;

/**
* @var ArticleResourceItemFactory
*/
protected $articleResourceItemFactory;

/**
* @var string
*/
Expand All @@ -70,6 +75,7 @@ class ArticleDataProvider implements DataProviderInterface, DataProviderAliasInt
* @param DocumentManagerInterface $documentManager
* @param LazyLoadingValueHolderFactory $proxyFactory
* @param ReferenceStoreInterface $referenceStore
* @param ArticleResourceItemFactory $articleResourceItemFactory
* @param string $articleDocumentClass
* @param int $defaultLimit
*/
Expand All @@ -78,13 +84,15 @@ public function __construct(
DocumentManagerInterface $documentManager,
LazyLoadingValueHolderFactory $proxyFactory,
ReferenceStoreInterface $referenceStore,
ArticleResourceItemFactory $articleResourceItemFactory,
$articleDocumentClass,
$defaultLimit
) {
$this->searchManager = $searchManager;
$this->documentManager = $documentManager;
$this->proxyFactory = $proxyFactory;
$this->referenceStore = $referenceStore;
$this->articleResourceItemFactory = $articleResourceItemFactory;
$this->articleDocumentClass = $articleDocumentClass;
$this->defaultLimit = $defaultLimit;
}
Expand Down Expand Up @@ -180,10 +188,7 @@ public function resolveResourceItems(
$this->referenceStore->add($document->getUuid());

$uuids[] = $document->getUuid();
$result[] = new ArticleResourceItem(
$document,
$this->getResource($document->getUuid(), $document->getLocale())
);
$result[] = $this->articleResourceItemFactory->getResourceItem($document);
}

return new DataProviderResult($result, $this->hasNextPage($queryResult, $limit, $page, $pageSize), $uuids);
Expand Down
10 changes: 10 additions & 0 deletions Content/ArticleResourceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ public function getPublished()
return $this->article->getPublished();
}

/**
* Returns authored.
*
* @return \DateTime
*/
public function getAuthored()
{
return $this->article->getAuthored();
}

/**
* Returns excerpt.
*
Expand Down
85 changes: 85 additions & 0 deletions Content/ArticleResourceItemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Content;

use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Component\DocumentManager\DocumentManagerInterface;

class ArticleResourceItemFactory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpdoc also on classes and interfaces

{
/**
* @var DocumentManagerInterface
*/
protected $documentManager;

/**
* @var LazyLoadingValueHolderFactory
*/
protected $proxyFactory;

/**
* @param DocumentManagerInterface $documentManager
* @param LazyLoadingValueHolderFactory $proxyFactory
*/
public function __construct(
DocumentManagerInterface $documentManager,
LazyLoadingValueHolderFactory $proxyFactory
) {
$this->documentManager = $documentManager;
$this->proxyFactory = $proxyFactory;
}

/**
* Creates and returns article source item with proxy document.
*
* @param ArticleViewDocumentInterface $articleViewDocument
*
* @return ArticleResourceItem
*/
public function getResourceItem(ArticleViewDocumentInterface $articleViewDocument)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use createResurceItem - factory creates items

{
return new ArticleResourceItem(
$articleViewDocument,
$this->getResource($articleViewDocument->getUuid(), $articleViewDocument->getLocale())
);
}

/**
* Returns Proxy document for uuid.
*
* @param string $uuid
* @param string $locale
*
* @return object
*/
private function getResource($uuid, $locale)
{
return $this->proxyFactory->createProxy(
ArticleDocument::class,
function (
&$wrappedObject,
LazyLoadingInterface $proxy,
$method,
array $parameters,
&$initializer
) use ($uuid, $locale) {
$initializer = null;
$wrappedObject = $this->documentManager->find($uuid, $locale);

return true;
}
);
}
}
11 changes: 11 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public function getConfigTreeBuilder()
->end()
->end()
->scalarNode('display_tab_all')->defaultTrue()->info("Display tab 'all' in list view")->end()
->arrayNode('search_fields')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update config:dump in installation.md

->prototype('scalar')->end()->defaultValue([
'title',
'excerpt.title',
'excerpt.description',
'excerpt.seo.title',
'excerpt.seo.description',
'excerpt.seo.keywords',
'teaser_description',
])
->end()
->end();

return $treeBuilder;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand Down Expand Up @@ -139,6 +138,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('sulu_article.view_document.article.class', $config['documents']['article']['view']);
$container->setParameter('sulu_article.display_tab_all', $config['display_tab_all']);
$container->setParameter('sulu_article.smart_content.default_limit', $config['smart_content']['default_limit']);
$container->setParameter('sulu_article.search_fields', $config['search_fields']);

$container->setParameter(
'sulu_article.content-type.article.template',
Expand Down
146 changes: 146 additions & 0 deletions Document/Repository/ArticleViewDocumentRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Document\Repository;

use ONGR\ElasticsearchBundle\Result\DocumentIterator;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchBundle\Service\Repository;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\Specialized\MoreLikeThisQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Component\DocumentManager\DocumentManagerInterface;

class ArticleViewDocumentRepository
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpdoc

{
use ArticleViewDocumentIdTrait;

/**
* @var Manager
*/
protected $searchManager;

/**
* @var DocumentManagerInterface
*/
protected $documentManager;

/**
* @var string
*/
protected $articleDocumentClass;

/**
* @var Repository
*/
protected $repository;

/**
* @var array
*/
protected $searchFields;

/**
* @param Manager $searchManager
* @param string $articleDocumentClass
* @param array $searchFields
*/
public function __construct(
Manager $searchManager,
$articleDocumentClass,
array $searchFields
) {
$this->searchManager = $searchManager;
$this->articleDocumentClass = $articleDocumentClass;
$this->repository = $this->searchManager->getRepository($this->articleDocumentClass);
$this->searchFields = $searchFields;
}

/**
* Finds recent articles for given parameters sorted by field `published`.
*
* @param null|string $excludeUuid
* @param int $limit
* @param null|array $types
* @param string $locale
*
* @return DocumentIterator
*/
public function findRecent($excludeUuid = null, $limit, array $types = null, $locale)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide a default for limit

{
$search = $this->createSearch($limit, $types, $locale);

if ($excludeUuid) {
$search->addQuery(new TermQuery('uuid', $excludeUuid), BoolQuery::MUST_NOT);
}

$search->addSort(new FieldSort('published'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm in my opinion should be authored


return $this->repository->findDocuments($search);
}

/**
* Finds similar articles for given `uuid` with given parameters.
*
* @param string $uuid
* @param int $limit
* @param null|array $types
* @param string $locale
*
* @return DocumentIterator
*/
public function findSimilar($uuid, $limit, array $types = null, $locale)
{
$search = $this->createSearch($limit, $types, $locale);

$search->addQuery(
new MoreLikeThisQuery(
null,
[
'fields' => $this->searchFields,
'min_term_freq' => 1,
'min_doc_freq' => 2,
'ids' => [$this->getViewDocumentId($uuid, $locale)],
]
)
);

return $this->repository->findDocuments($search);
}

/**
* @param int $limit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always add a description for function

* @param null|array $types
* @param string $locale
*
* @return Search
*/
private function createSearch($limit, array $types = null, $locale)
{
$search = $this->repository->createSearch();

$search->addQuery(new TermQuery('locale', $locale), BoolQuery::FILTER);
$search->setSize($limit);

if ($types) {
$typesQuery = new BoolQuery();
foreach ($types as $type) {
$typesQuery->add(new TermQuery('type', $type), BoolQuery::SHOULD);
}
$search->addQuery($typesQuery);
}

return $search;
}
}
23 changes: 23 additions & 0 deletions Exception/ArticleInRequestNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Exception;

/**
* Thrown when no article-page is found in request.
*/
class ArticleInRequestNotFoundException extends \Exception
{
public function __construct()
{
parent::__construct('No article in request found');
}
}
Loading