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

Make extensible #8

Open
wants to merge 7 commits into
base: gluing-together-more
from

Abstract content getter selection out so that it may become extensibl…

…e, but ensure default behavior is kept.
  • Loading branch information...
Shelob9 committed May 29, 2018
commit a4ea96c07884a396e89c5d0bf01260f2dcb9c9d3
@@ -0,0 +1,39 @@
<?php
namespace CalderaLearn\RestSearch\Tests\Unit;
use CalderaLearn\RestSearch\ContentGetter\PostsGenerator;
use CalderaLearn\RestSearch\Modes;
class ModeControlTests extends TestCase
{
/**
* Test that the right type of ContentGetters come out of factory
*
* @covers \CalderaLearn\RestSearch\Modes::factory()
*/
public function testFactory()
{
$modes = new Modes();
$this->assertTrue(is_a($modes->factory(''), PostsGenerator::class));
$this->assertTrue(is_a($modes->factory(PostsGenerator::class), PostsGenerator::class));
}
/**
* Test that the right type of ContentGetters come out of factory
*
* @covers \CalderaLearn\RestSearch\Modes::factory()
*/
public function testGetMode()
{
$modes = new Modes();
$mockRestRequest = \Mockery::mock('\WP_Rest_Request');
$mockRestRequest->shouldReceive('get_param')
->once()
->andReturn('default');
$this->assertTrue(is_a($modes->controlMode(nulll, $mockRestRequest), PostsGenerator::class));
}
}
@@ -135,15 +135,15 @@ public static function getPosts(WP_Query $query): array
{
$request = is_object(static::$request) ? static::$request : new \WP_REST_Request();
/**
* Runs right before we get the results
*
* Use to change the contentGetter based on current request
*
* @param \WP_Query $query
* @param \WP_REST_Request $request
*/
do_action( 'caldera_learn_rest_search_pre_get_posts', $query, $request );
/**
* Runs right before we get the results
*
* Use to change the contentGetter based on current request
*
* @param \WP_Query $query
* @param \WP_REST_Request $request
*/
do_action('caldera_learn_rest_search_pre_get_posts', $query, $request);
return static::$contentGetter->getContent($query, $request);
}
}
@@ -12,12 +12,18 @@
*/
class Hooks
{
/**
* @var Modes
*/
protected $modes;
/**
* Add all hooks used by this plugin
*/
public function addHooks()
{
FilterWPQuery::addFilter();
$this->modes = new Modes();
add_filter('caldera_learn_rest_search_pre_get_posts', [ $this->modes, 'controlMode' ], 10, 2);
}
/**
@@ -26,5 +32,6 @@ public function addHooks()
public function removeHooks()
{
FilterWPQuery::removeFilter();
remove_filter('caldera_learn_rest_search_pre_get_posts', [ $this->modes, 'controlMode' ], 10);
}
}
@@ -0,0 +1,45 @@
<?php
namespace CalderaLearn\RestSearch;
use CalderaLearn\RestSearch\ContentGetter\ContentGetterContract;
use CalderaLearn\RestSearch\ContentGetter\PostsGenerator;
class Modes
{
/**
* Controls loading of content generator based on mode
*
* @param \WP_Query $unused
* @param \WP_REST_Request $request
* @return ContentGetterContract
*/
public function controlMode($unused, \WP_REST_Request $request) : ContentGetterContract
{
switch ($request->get_param('mode')) {
default:
return $this->factory('');
break;

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

This isn't necessary as we are returning and the break will never be reached.

}
}
/**
* Factory for contentGetter with cache
*
* @todo cache
*
* @param string $type
* @return ContentGetterContract
*/
public function factory(string $type) : ContentGetterContract
{
switch ($type) {

This comment has been minimized.

Copy link
@hellofromtonya

hellofromtonya Jun 2, 2018

Collaborator

Same as above, what is the point of this switch? In its current design, it will always create a new instance of the PostsGenerator and then return it. Why not just do line 57 and remove the rest of this code?

case PostsGenerator::class:
default:
return new PostsGenerator();
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.