...
 
......@@ -40,7 +40,7 @@ class views implements Interfaces\Api
// Boost Campaigns
try {
$campaign = $campaignsManager->get((string) $urn);
$campaign = $campaignsManager->getCampaignByUrn((string)$urn);
$campaignsMetricsManager
->setCampaign($campaign)
......
......@@ -50,7 +50,7 @@ class campaigns implements Interfaces\Api
$manager = Di::_()->get('Boost\Campaigns\Manager');
$manager->setActor(Session::getLoggedInUser());
$response = $manager->getList([
$response = $manager->getCampaigns([
'owner_guid' => Session::getLoggedinUserGuid(),
'limit' => $limit,
'offset' => $offset,
......@@ -106,9 +106,9 @@ class campaigns implements Interfaces\Api
try {
if (!$isEditing) {
$campaign = $manager->create($campaign, $_POST['payment'] ?? null);
$campaign = $manager->createCampaign($campaign, $_POST['payment'] ?? null);
} else {
$campaign = $manager->update($campaign, $_POST['payment'] ?? null);
$campaign = $manager->updateCampaign($campaign, $_POST['payment'] ?? null);
}
Factory::response([
......
......@@ -86,7 +86,7 @@ class campaigns implements Interfaces\Api
$data = [];
try {
$result = $manager->fetch([
$result = $manager->getCampaignsAndBoosts([
'limit' => $limit,
'from' => $offset,
'rating' => $rating,
......
......@@ -78,7 +78,7 @@ class TokenSaleEvent implements BlockchainEventInterface
return; //purchase not found
}
var_dump($log);
error_log(print_r($log, true));
//is the requested amount below what has already been recorded
if ($transaction->getAmount() > $purchase->getUnissuedAmount()) {
return; //requested more than can issue
......
......@@ -96,7 +96,6 @@ class WithdrawEvent implements BlockchainEventInterface
try {
$this->manager->complete($request, $transaction);
} catch (\Exception $e) {
var_dump($e);
error_log(print_r($e, true));
}
}
......
......@@ -47,7 +47,7 @@ class Dispatcher
public function onLifecycle(string $campaignUrn)
{
$this->now = time() * 1000;
$this->campaign = $this->manager->get($campaignUrn);
$this->campaign = $this->manager->getCampaignByUrn($campaignUrn);
$this->metrics->setCampaign($this->campaign);
$this->syncIfImpressionsThresholdMet();
......@@ -81,7 +81,7 @@ class Dispatcher
{
if ($this->campaign->shouldBeCompleted($this->now)) {
error_log("[BoostCampaignsDispatcher] Completing {$this->campaign->getUrn()}...");
$this->manager->complete($this->campaign);
$this->manager->completeCampaign($this->campaign);
}
}
......
......@@ -11,7 +11,6 @@ use Minds\Core\Data\ElasticSearch\Prepared\Update;
use Minds\Core\Di\Di;
use Minds\Helpers\Number;
use Minds\Helpers\Text;
use NotImplementedException;
class ElasticRepository
{
......@@ -39,7 +38,7 @@ class ElasticRepository
* @param array $opts
* @return Response
*/
public function getList(array $opts = [])
public function getCampaigns(array $opts = [])
{
$this->opts = array_merge([
'limit' => 12,
......@@ -98,7 +97,7 @@ class ElasticRepository
return $response;
}
public function fetch(array $opts = [])
public function getCampaignsAndBoosts(array $opts = [])
{
$this->opts = array_merge([
'limit' => 24,
......@@ -157,7 +156,7 @@ class ElasticRepository
* @return bool
* @throws Exception
*/
public function add(Campaign $campaign)
public function putCampaign(Campaign $campaign)
{
$body = [
'doc' => [
......@@ -217,23 +216,4 @@ class ElasticRepository
return (bool) $this->es->request($prepared);
}
/**
* @param Campaign $campaign
* @return bool
* @throws Exception
*/
public function update(Campaign $campaign)
{
return $this->add($campaign);
}
/**
* @param Campaign $campaign
* @throws NotImplementedException
*/
public function delete(Campaign $campaign)
{
throw new NotImplementedException();
}
}
......@@ -130,7 +130,7 @@ class Iterator implements \Iterator
public function getList()
{
$response = $this->manager->getList([
$response = $this->manager->getCampaigns([
'limit' => $this->limit,
'from' => $this->from,
'offset' => $this->offset,
......
......@@ -96,19 +96,20 @@ class Manager
}
/**
* Get a list of boost campaigns
* @param array $opts
* @return Response
* @throws Exception
*/
public function getList(array $opts = [])
public function getCampaigns(array $opts = [])
{
$opts = array_merge([
'useElastic' => true
], $opts);
$response = $opts['useElastic'] ?
$this->elasticRepository->getList($opts) :
$this->repository->getList($opts);
$this->elasticRepository->getCampaigns($opts) :
$this->repository->getCampaignByGuid($opts);
return $response->map(function (Campaign $campaign) {
try {
......@@ -128,15 +129,16 @@ class Manager
}
/**
* Get a list of boosts and boost campaigns
* @param array $opts
* @return Response|null
*/
public function fetch(array $opts = [])
public function getCampaignsAndBoosts(array $opts = [])
{
$response = null;
try {
$response = $this->elasticRepository->fetch($opts);
$response = $this->elasticRepository->getCampaignsAndBoosts($opts);
} catch (Exception $e) {
error_log("[BoostCampaignsManager] {$e}");
}
......@@ -145,12 +147,13 @@ class Manager
}
/**
* Get a single boost campaign by URN string
* @param string $urn
* @param array $opts
* @return Campaign|null
* @throws Exception
*/
public function get(string $urn, array $opts = [])
public function getCampaignByUrn(string $urn, array $opts = [])
{
$opts = array_merge([
'useElastic' => false
......@@ -162,7 +165,7 @@ class Manager
return null;
}
$campaigns = $this->getList([
$campaigns = $this->getCampaigns([
'guid' => $guid,
'useElastic' => $opts['useElastic'],
])->toArray();
......@@ -180,7 +183,7 @@ class Manager
* @return Campaign
* @throws CampaignException
*/
public function create(Campaign $campaign, $paymentPayload = null)
public function createCampaign(Campaign $campaign, $paymentPayload = null)
{
$campaign = $this->campaignUrnDelegate->onCreate($campaign);
$campaign->setOwner($this->actor);
......@@ -209,8 +212,8 @@ class Manager
$campaign = $this->normalizeHashtagsDelegate->onCreate($campaign);
$campaign = $this->paymentsDelegate->onCreate($campaign, $paymentPayload);
$this->repository->add($campaign);
$this->elasticRepository->add($campaign);
$this->repository->putCampaign($campaign);
$this->elasticRepository->putCampaign($campaign);
return $campaign;
}
......@@ -222,9 +225,9 @@ class Manager
* @throws CampaignException
* @throws Exception
*/
public function update(Campaign $campaignRef, $paymentPayload = null)
public function updateCampaign(Campaign $campaignRef, $paymentPayload = null)
{
$campaign = $this->get($campaignRef->getUrn());
$campaign = $this->getCampaignByUrn($campaignRef->getUrn());
$isOwner = (string) $this->actor->guid === (string) $campaign->getOwnerGuid();
if ($this->actor && !$this->actor->isAdmin() && !$isOwner) {
......@@ -256,8 +259,8 @@ class Manager
*/
public function sync(Campaign $campaign): Campaign
{
$this->repository->update($campaign, true);
$this->elasticRepository->update($campaign);
$this->repository->putCampaign($campaign);
$this->elasticRepository->putCampaign($campaign);
return $campaign;
}
......@@ -277,12 +280,12 @@ class Manager
*/
public function start(Campaign $campaignRef)
{
$campaign = $this->get($campaignRef->getUrn());
if ($this->actor) {
throw new CampaignException('Campaigns should not be manually started');
}
$campaign = $this->getCampaignByUrn($campaignRef->getUrn());
if ($campaign->getDeliveryStatus() !== Campaign::STATUS_CREATED) {
throw new CampaignException('Campaign should be in [created] state in order to start it');
}
......@@ -299,13 +302,9 @@ class Manager
* @throws CampaignException
* @throws Exception
*/
public function cancel(Campaign $campaignRef)
public function cancelCampaign(Campaign $campaignRef)
{
$campaign = $this->get($campaignRef->getUrn());
if (!$campaign) {
throw new CampaignException('Campaign does not exist');
}
$campaign = $this->getCampaignByUrn($campaignRef->getUrn());
$isOwner = (string) $this->actor->guid === (string) $campaign->getOwnerGuid();
......@@ -332,9 +331,9 @@ class Manager
* @throws CampaignException
* @throws Exception
*/
public function reject(Campaign $campaignRef)
public function rejectCampaign(Campaign $campaignRef)
{
$campaign = $this->get($campaignRef->getUrn());
$campaign = $this->getCampaignByUrn($campaignRef->getUrn());
if ($this->actor && !$this->actor->isAdmin()) {
throw new CampaignException('You\'re not allowed to reject this campaign');
......@@ -358,9 +357,9 @@ class Manager
* @throws CampaignException
* @throws Exception
*/
public function complete(Campaign $campaignRef)
public function completeCampaign(Campaign $campaignRef)
{
$campaign = $this->get($campaignRef->getUrn());
$campaign = $this->getCampaignByUrn($campaignRef->getUrn());
if ($this->actor) {
throw new CampaignException('Campaigns should not be manually completed');
......@@ -384,11 +383,10 @@ class Manager
*/
public function onPaymentSuccess(Payment $paymentRef)
{
$campaign = $this->get("urn:campaign:{$paymentRef->getCampaignGuid()}");
$campaign = $this->getCampaignByUrn("urn:campaign:{$paymentRef->getCampaignGuid()}");
$campaign = $this->paymentsDelegate->onConfirm($campaign, $paymentRef);
$this->repository->update($campaign);
$this->elasticRepository->update($campaign);
$this->sync($campaign);
$this->sendToQueue($campaign);
}
......@@ -398,11 +396,10 @@ class Manager
*/
public function onPaymentFailed(Payment $paymentRef)
{
$campaign = $this->get("urn:campaign:{$paymentRef->getCampaignGuid()}");
$campaign = $this->getCampaignByUrn("urn:campaign:{$paymentRef->getCampaignGuid()}");
$campaign = $this->paymentsDelegate->onFail($campaign, $paymentRef);
$this->repository->update($campaign);
$this->elasticRepository->update($campaign);
$this->sync($campaign);
}
protected function sendToQueue(Campaign $campaign): void
......
......@@ -11,7 +11,6 @@ use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Core\Di\Di;
use Minds\Helpers\Number;
use Minds\Helpers\Text;
use NotImplementedException;
class Repository
{
......@@ -32,7 +31,7 @@ class Repository
* @return Response
* @throws Exception
*/
public function getList(array $opts = [])
public function getCampaignByGuid(array $opts = [])
{
$opts = array_merge([
'limit' => 12,
......@@ -117,7 +116,7 @@ class Repository
* @param bool $async
* @return bool
*/
public function add(Campaign $campaign, $async = true)
public function putCampaign(Campaign $campaign, $async = true)
{
$cql = "INSERT INTO boost_campaigns (type, guid, owner_guid, json_data, delivery_status) VALUES (?, ?, ?, ?, ?)";
......@@ -134,23 +133,4 @@ class Repository
return (bool) $this->db->request($prepared, $async);
}
/**
* @param Campaign $campaign
* @param bool $async
* @return bool
*/
public function update(Campaign $campaign, $async = true)
{
return $this->add($campaign, $async);
}
/**
* @param Campaign $campaign
* @throws NotImplementedException
*/
public function delete(Campaign $campaign)
{
throw new NotImplementedException();
}
}
......@@ -60,7 +60,7 @@ class BoostCampaignResolverDelegate implements ResolverDelegate
foreach ($urns as $urn) {
/** @var Campaign $campaign */
$campaign = $manager->get($urn);
$campaign = $manager->getCampaignByUrn($urn);
$entities[] = $campaign;
}
......
......@@ -38,7 +38,7 @@ class DispatcherSpec extends ObjectBehavior
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->get('urn:campaign:1000')
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($initialCampaign);
......@@ -84,7 +84,7 @@ class DispatcherSpec extends ObjectBehavior
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->get('urn:campaign:1000')
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($initialCampaign);
......@@ -132,7 +132,7 @@ class DispatcherSpec extends ObjectBehavior
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->get('urn:campaign:1000')
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($campaign);
......@@ -149,7 +149,7 @@ class DispatcherSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn(true);
$this->manager->complete($campaign)
$this->manager->completeCampaign($campaign)
->shouldBeCalled()
->willReturn(true);
......@@ -165,7 +165,7 @@ class DispatcherSpec extends ObjectBehavior
$campaign->getUrn()
->willReturn('urn:campaign:1000');
$this->manager->get('urn:campaign:1000')
$this->manager->getCampaignByUrn('urn:campaign:1000')
->shouldBeCalled()
->willReturn($campaign);
......
......@@ -65,7 +65,7 @@ class ElasticRepositorySpec extends ObjectBehavior
$this->queryBuilder->query()->shouldBeCalled();
$this->client->request(Argument::type(Prepared\Search::class))->shouldBeCalled()->willReturn($esResult);
$return = $this->getList(['guid' => 1234]);
$return = $this->getCampaigns(['guid' => 1234]);
$return[0]->shouldBeAnInstanceOf(Campaign::class);
}
......@@ -100,12 +100,12 @@ class ElasticRepositorySpec extends ObjectBehavior
$this->queryBuilder->query()->shouldBeCalled();
$this->client->request(Argument::type(Prepared\Search::class))->shouldBeCalled()->willReturn($esResult);
$return = $this->fetch(['quality' => 5]);
$return = $this->getCampaignsAndBoosts(['quality' => 5]);
$return[0]->shouldBeAnInstanceOf(Campaign::class);
$return[1]->shouldBeAnInstanceOf(Boost::class);
}
public function it_should_add_an_entry_to_es(Campaign $campaign)
public function it_should_put_a_campaign_to_es(Campaign $campaign)
{
$campaign->getType()->willReturn('newsfeed');
$campaign->getOwnerGuid()->willReturn('1234');
......@@ -130,39 +130,6 @@ class ElasticRepositorySpec extends ObjectBehavior
$campaign->getGuid()->willReturn('12345');
$this->client->request(Argument::type(Prepared\Update::class))->shouldBeCalled()->willReturn(true);
$this->add($campaign);
}
public function it_should_update_an_entry_to_es(Campaign $campaign)
{
$campaign->getType()->willReturn('newsfeed');
$campaign->getOwnerGuid()->willReturn('1234');
$campaign->getName()->willReturn('test');
$campaign->getEntityUrns()->willReturn(null);
$campaign->getHashtags()->willReturn(null);
$campaign->getNsfw()->willReturn(null);
$campaign->getStart()->willReturn('12345');
$campaign->getEnd()->willReturn('23456');
$campaign->getBudget()->willReturn('1');
$campaign->getBudgetType()->willReturn('tokens');
$campaign->getChecksum()->willReturn('0xdeadbeef');
$campaign->getImpressions()->willReturn(null);
$campaign->getCreatedTimestamp()->willReturn('123456');
$campaign->getImpressionsMet()->willReturn('2');
$campaign->getRating()->willReturn('5');
$campaign->getQuality()->willReturn('5');
$campaign->getReviewedTimestamp()->willReturn(null);
$campaign->getRejectedTimestamp()->willReturn(null);
$campaign->getRevokedTimestamp()->willReturn(null);
$campaign->getCompletedTimestamp()->willReturn(null);
$campaign->getGuid()->willReturn('12345');
$this->client->request(Argument::type(Prepared\Update::class))->shouldBeCalled()->willReturn(true);
$this->update($campaign);
}
public function it_should_delete_entry_in_es()
{
$this->shouldThrow('NotImplementedException')->duringDelete(new Campaign());
$this->putCampaign($campaign);
}
}
This diff is collapsed.
......@@ -62,28 +62,20 @@ class RepositorySpec extends ObjectBehavior
'owner_guid' => 1234,
'type' => 'newsfeed'
],
[
'guid' => 1235,
'delivery_status' => '',
'json_data' => json_encode($this->boostCampaignJsonDataMock),
'owner_guid' => 1235,
'type' => 'newsfeed'
],
], '');
$this->client->request(Argument::type(Prepared\Custom::class))
->shouldBeCalled()
->willReturn($rows);
$return = $this->getList(['guid' => 1234]);
$return = $this->getCampaignByGuid(['guid' => 1234]);
$return[0]->shouldBeAnInstanceOf(Campaign::class);
$return[1]->shouldBeAnInstanceOf(Campaign::class);
}
public function it_should_not_return_a_list_if_no_args_passed()
{
$this->shouldThrow('Exception')->duringGetList();
$this->shouldThrow('Exception')->duringGetCampaignByGuid();
}
public function it_should_store_a_boost_campaign()
......@@ -100,7 +92,7 @@ class RepositorySpec extends ObjectBehavior
->shouldBeCalled()
->willReturn(true);
$this->add($campaign)->shouldReturn(true);
$this->putCampaign($campaign)->shouldReturn(true);
}
public function it_should_update_a_boost_campaign()
......@@ -117,11 +109,6 @@ class RepositorySpec extends ObjectBehavior
->shouldBeCalled()
->willReturn(true);
$this->update($campaign)->shouldReturn(true);
}
public function it_should_throw_not_implemented_exception_on_delete_a_boost_campaign()
{
$this->shouldThrow('NotImplementedException')->during('delete', [new Campaign()]);
$this->putCampaign($campaign)->shouldReturn(true);
}
}