...
 
......@@ -224,7 +224,7 @@ class boost implements Interfaces\Api
// Validate entity
$boostHandler = Core\Boost\Handler\Factory::getHandler(ucfirst($pages[0]));
$boostHandler = Core\Boost\Handler\Factory::get($pages[0]);
$isEntityValid = $boostHandler->validateEntity($entity);
if (!$isEntityValid) {
......
......@@ -28,18 +28,10 @@ class BoostProvider extends Di\Provider
return new Network\Iterator();
}, ['useFactory' => false]);
$this->di->bind('Boost\Network\Metrics', function ($di) {
return new Network\Metrics();
}, ['useFactory' => false]);
$this->di->bind('Boost\Network\Review', function ($di) {
return new Network\Review();
}, ['useFactory' => false]);
$this->di->bind('Boost\Peer', function ($di) {
return new Peer();
}, ['useFactory' => true]);
$this->di->bind('Boost\Peer\Review', function ($di) {
return new Peer\Review();
}, ['useFactory' => false]);
......
......@@ -9,13 +9,32 @@ use Minds\Interfaces\BoostHandlerInterface;
*/
class Factory
{
public static function getHandler($handler): BoostHandlerInterface
const HANDLER_CHANNEL = 'channel';
const HANDLER_CONTENT = 'content';
const HANDLER_NETWORK = 'network';
const HANDLER_NEWSFEED = 'newsfeed';
const HANDLER_PEER = 'peer';
const HANDLERS = [
self::HANDLER_CHANNEL => Channel::class,
self::HANDLER_CONTENT => Content::class,
self::HANDLER_NETWORK => Network::class,
self::HANDLER_NEWSFEED => Newsfeed::class,
self::HANDLER_PEER => Peer::class
];
/**
* @param string $handler
* @return BoostHandlerInterface
* @throws \Exception
*/
public static function get(string $handler): BoostHandlerInterface
{
$handler = ucfirst($handler);
$handler = "Minds\\Core\\Boost\\Handler\\$handler";
if (class_exists($handler)) {
return new $handler;
if (!isset(self::HANDLERS[$handler]) || !class_exists(self::HANDLERS[$handler])) {
throw new \Exception("Handler not found");
}
throw new \Exception("Handler not found");
$class = self::HANDLERS[$handler];
return new $class;
}
}
......@@ -7,18 +7,6 @@ use Minds\Helpers;
class Metrics
{
protected $type;
/**
* @param string $type
* @return $this
*/
public function setType($type)
{
$this->type = strtolower($type);
return $this;
}
/**
* Increments impressions to a given boost
* @param Network $boost
......
<?php
namespace Spec\Minds\Core\Boost;
namespace Spec\Minds\Core\Boost\Handler;
use Minds\Core\Boost\Handler\Factory;
use Minds\Core\Boost\Handler\Newsfeed;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Minds\Core\Data\MongoDB\Client as MongoClient;
class FactorySpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Boost\Factory');
$this->shouldHaveType(Factory::class);
}
public function it_should_build_a_handler(MongoClient $db)
public function it_should_return_a_handler()
{
$this::build("Newsfeed", Argument::any(), $db)->shouldHaveType('Minds\Core\Boost\Newsfeed');
$this::get(Factory::HANDLER_NEWSFEED)->shouldHaveType(Newsfeed::class);
}
public function it_should_throw_an_error_if_handler_doesnt_exist()
{
$this->shouldThrow("\Exception")->during("build", ["FakeBoost"]);
$this->shouldThrow("\Exception")->during("get", ["FakeBoost"]);
}
}
<?php
namespace Spec\Minds\Core\Boost\Handler;
use Minds\Core\Blogs\Blog;
use Minds\Core\Boost\Handler\Newsfeed;
use Minds\Entities\Activity;
use Minds\Entities\Image;
use Minds\Entities\User;
use Minds\Entities\Video;
use PhpSpec\ObjectBehavior;
class NewsfeedSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Newsfeed::class);
}
public function it_should_validate_entity(
Activity $activity,
Video $video,
Image $image,
Blog $blog,
User $user)
{
$this->validateEntity($activity)->shouldReturn(true);
$this->validateEntity($video)->shouldReturn(true);
$this->validateEntity($image)->shouldReturn(true);
$this->validateEntity($blog)->shouldReturn(true);
$this->validateEntity($user)->shouldReturn(false);
}
}
......@@ -2,56 +2,13 @@
namespace Spec\Minds\Core\Boost\Network;
use Minds\Core\Boost\Repository;
use Minds\Core\Data\MongoDB;
use Minds\Core\Di\Di;
use Minds\Core\Boost\Network\Metrics;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MetricsSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Boost\Network\Metrics');
}
public function it_should_get_backlog_count(MongoDB\Client $mongo)
{
$mongo->count(Argument::containingString('boost'), Argument::any())
->shouldBeCalled()
->willReturn(3);
$this->beConstructedWith($mongo);
$this->getBacklogCount('newsfeed', '123')->shouldReturn(3);
}
public function it_should_get_priority_backlog_count(MongoDB\Client $mongo)
{
$mongo->count(Argument::containingString('boost'), Argument::any())
->shouldBeCalled()
->willReturn(3);
$this->beConstructedWith($mongo);
$this->getPriorityBacklogCount('newsfeed', '123')->shouldReturn(3);
}
public function it_should_get_backlog_impressions_sum(MongoDB\Client $mongo)
{
$total = new \stdClass();
$total->total = 10;
$result = ['total' => $total];
$mongo->aggregate(Argument::containingString('boost'), Argument::any())
->shouldBeCalled()
->willReturn($result);
$this->beConstructedWith($mongo);
$this->getBacklogImpressionsSum('newsfeed')->shouldReturn(10);
$this->shouldHaveType(Metrics::class);
}
}
<?php
namespace Spec\Minds\Core\Boost;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Prophecy\Prophet;
use Minds\Entities\User;
use Minds\Entities\Boost\Network;
use Minds\Core\Data\Call;
use Minds\Core\Data\MongoDB\Client;
use Minds\Core\Data\Interfaces\ClientInterface;
class NewsfeedSpec extends ObjectBehavior
{
public function let(Client $mongo, Call $db, User $user)
{
$mongo->insert(Argument::type('string'), Argument::type('array'))
->willReturn("boost_id");
//$db->getRow(Argument::type(''))->will
$this->beConstructedWith([], $mongo, $db);
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Boost\Newsfeed');
}
public function it_can_boost_a_post(Network $boost, User $user, Client $mongo, Call $db)
{
$boost->getGuid()->willReturn('foo');
$boost->getImpressions()->willReturn(10);
$boost->getOwner()->willReturn($user);
$boost->getRating()->willReturn(1);
$boost->getQuality()->willReturn(75);
$boost->getImpressions()->willReturn(100);
$boost->getPriorityRate()->willReturn(0);
$boost->getCategories()->willReturn(['art', 'music']);
$this->boost($boost)->shouldBeString();
}
}
......@@ -245,54 +245,6 @@ class RepositorySpec extends ObjectBehavior
->shouldReturn(false);
}
// getEntityById()
public function it_should_get_a_single_boost_based_on_mongo()
{
$this->_client->request(Argument::type(Prepared\Custom::class))
->shouldBeCalled()
->willReturn([
[ 'type' => 'network', 'data' => [ ] ],
]);
$this
->getEntityById('network', 'm2000')
->shouldReturnAnInstanceOf(Entities\Boost\Network::class);
}
public function it_should_not_get_a_single_boost_based_on_mongo_if_no_type()
{
$this->_client->request(Argument::type(Prepared\Custom::class))
->shouldNotBeCalled();
$this
->getEntityById(null, 'm2000')
->shouldReturn(false);
}
public function it_should_not_get_a_single_boost_based_on_mongo_if_no_id()
{
$this->_client->request(Argument::type(Prepared\Custom::class))
->shouldNotBeCalled();
$this
->getEntityById('network', null)
->shouldReturn(false);
}
public function it_should_not_get_a_single_boost_based_on_mongo_if_not_exists()
{
$this->_client->request(Argument::type(Prepared\Custom::class))
->shouldBeCalled()
->willReturn([]);
$this
->getEntityById('network', 'm2404')
->shouldReturn(false);
}
// upsert()
public function it_should_store_a_boost()
{
$this->_client->request(Argument::type(Prepared\Custom::class))
......