Skip to content

Commit 2aa66bd

Browse files
maximehurandelyriand
authored andcommitted
Search is available if we have at least one documentable registry
1 parent 6460816 commit 2aa66bd

File tree

6 files changed

+98
-31
lines changed

6 files changed

+98
-31
lines changed

src/Checker/ElasticsearchChecker.php

+4-23
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,18 @@
1313

1414
namespace MonsieurBiz\SyliusSearchPlugin\Checker;
1515

16-
use MonsieurBiz\SyliusSearchPlugin\Search\ClientFactory;
17-
use Sylius\Component\Locale\Context\LocaleContextInterface;
18-
use Sylius\Component\Registry\ServiceRegistryInterface;
16+
use JoliCode\Elastically\Factory;
1917

20-
class ElasticsearchChecker
18+
class ElasticsearchChecker implements ElasticsearchCheckerInterface
2119
{
22-
private ClientFactory $clientFactory;
23-
24-
private ServiceRegistryInterface $documentableRegistry;
25-
26-
private LocaleContextInterface $localeContext;
27-
2820
private ?bool $isAvailable = null;
2921

30-
public function __construct(
31-
ClientFactory $clientFactory,
32-
ServiceRegistryInterface $documentableRegistry,
33-
LocaleContextInterface $localeContext
34-
) {
35-
$this->clientFactory = $clientFactory;
36-
$this->documentableRegistry = $documentableRegistry;
37-
$this->localeContext = $localeContext;
38-
}
39-
4022
public function check(): bool
4123
{
4224
if (null === $this->isAvailable) {
43-
$documentables = $this->documentableRegistry->all();
44-
$documentable = reset($documentables);
45-
$client = $this->clientFactory->getClient($documentable, $this->localeContext->getLocaleCode());
25+
$client = (new Factory())->buildClient();
4626

27+
// Check client response
4728
try {
4829
$client->getStatus()->getResponse();
4930
} catch (\Exception $e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz' Search plugin for Sylius.
5+
*
6+
* (c) Monsieur Biz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace MonsieurBiz\SyliusSearchPlugin\Checker;
15+
16+
interface ElasticsearchCheckerInterface
17+
{
18+
public function check(): bool;
19+
}

src/Resources/config/routing/shop.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ monsieurbiz_search_search:
55
_controller: MonsieurBiz\SyliusSearchPlugin\Controller\SearchController::searchAction
66
requirements:
77
query: .+
8-
condition: service('monsieurbiz.search.checker.elasticsearch_checker').check()
8+
condition: "not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and context.checkElasticsearch()"
99

1010
monsieurbiz_search_post:
1111
path: /search
@@ -28,4 +28,4 @@ monsieurbiz_sylius_search_taxon:
2828
taxon: "expr:notFoundOnNull(service('sylius.repository.taxon').findOneBySlug($slug, service('sylius.context.locale').getLocaleCode()))"
2929
requirements:
3030
slug: .+
31-
condition: service('monsieurbiz.search.checker.elasticsearch_checker').check()
31+
condition: "not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and context.checkElasticsearch()"

src/Resources/config/services.yaml

+7-3
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,14 @@ services:
255255

256256
monsieurbiz.search.checker.elasticsearch_checker:
257257
class: MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchChecker
258-
public: true
259-
tags:
260-
- { name: routing.condition_service }
261258

262259
MonsieurBiz\SyliusSearchPlugin\Twig\Extension\RenderSearchForm:
263260
arguments:
264261
$elasticsearchChecker: '@monsieurbiz.search.checker.elasticsearch_checker'
262+
263+
# Routing Context
264+
MonsieurBiz\SyliusSearchPlugin\Routing\RequestContext:
265+
decorates: router.request_context
266+
arguments:
267+
- '@MonsieurBiz\SyliusSearchPlugin\Routing\RequestContext.inner'
268+
- '@monsieurbiz.search.checker.elasticsearch_checker'

src/Routing/RequestContext.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Monsieur Biz' Search plugin for Sylius.
5+
*
6+
* (c) Monsieur Biz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace MonsieurBiz\SyliusSearchPlugin\Routing;
15+
16+
use Exception;
17+
use MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchCheckerInterface;
18+
use Symfony\Component\Routing\RequestContext as BaseRequestContext;
19+
20+
class RequestContext extends BaseRequestContext
21+
{
22+
private BaseRequestContext $decorated;
23+
24+
private ElasticsearchCheckerInterface $elasticsearchChecker;
25+
26+
public function __construct(
27+
BaseRequestContext $decorated,
28+
ElasticsearchCheckerInterface $elasticsearchChecker
29+
) {
30+
parent::__construct(
31+
$decorated->getBaseUrl(),
32+
$decorated->getMethod(),
33+
$decorated->getHost(),
34+
$decorated->getScheme(),
35+
$decorated->getHttpPort(),
36+
$decorated->getHttpsPort(),
37+
$decorated->getPathInfo(),
38+
$decorated->getQueryString()
39+
);
40+
$this->decorated = $decorated;
41+
$this->elasticsearchChecker = $elasticsearchChecker;
42+
}
43+
44+
public function checkElasticsearch(): bool
45+
{
46+
return $this->elasticsearchChecker->check();
47+
}
48+
49+
/**
50+
* @throws Exception
51+
*
52+
* @return mixed
53+
*/
54+
public function __call(string $name, array $arguments)
55+
{
56+
$callback = [$this->decorated, $name];
57+
if (\is_callable($callback)) {
58+
return \call_user_func($callback, $arguments);
59+
}
60+
61+
throw new Exception(sprintf('Method %s not found for class "%s"', $name, \get_class($this->decorated)));
62+
}
63+
}

src/Twig/Extension/RenderSearchForm.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace MonsieurBiz\SyliusSearchPlugin\Twig\Extension;
1515

16-
use MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchChecker;
16+
use MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchCheckerInterface;
1717
use MonsieurBiz\SyliusSearchPlugin\Form\Type\SearchType;
1818
use Symfony\Component\Form\FormFactoryInterface;
1919
use Symfony\Component\HttpFoundation\RequestStack;
@@ -30,13 +30,13 @@ class RenderSearchForm extends AbstractExtension
3030

3131
private RequestStack $requestStack;
3232

33-
private ElasticsearchChecker $elasticsearchChecker;
33+
private ElasticsearchCheckerInterface $elasticsearchChecker;
3434

3535
public function __construct(
3636
FormFactoryInterface $formFactory,
3737
Environment $templatingEngine,
3838
RequestStack $requestStack,
39-
ElasticsearchChecker $elasticsearchChecker
39+
ElasticsearchCheckerInterface $elasticsearchChecker
4040
) {
4141
$this->formFactory = $formFactory;
4242
$this->templatingEngine = $templatingEngine;

0 commit comments

Comments
 (0)