Commit 8ec1fcb2 authored by Ben's avatar Ben
Browse files

Updated to add spec tests

parent 51618b7d
Loading
Loading
Loading
Loading
+38 −8
Original line number Original line Diff line number Diff line
@@ -23,16 +23,13 @@ class Plus
    }
    }


    /**
    /**
     * On Wire
     * To be called on an incoming wire.
     * @param Wire $wire
     * @param Wire $wire - the wire object.
     * @param string $receiver_address
     * @param string $receiver_address - the recieving address.
     * @return Wire $wire
     * @return Wire $wire - the wire object.
     */
     */
    public function onWire($wire, $receiver_address, $tier = null)
    public function onWire($wire, $receiver_address, $tier = null)
    {
    {
        error_log("IN THE DELEGATE^^^^^^^");
        error_log(var_export($tier, true));
        die();
        if ($wire->getReceiver()->guid != $this->config->get('blockchain')['contracts']['wire']['plus_guid']) {
        if ($wire->getReceiver()->guid != $this->config->get('blockchain')['contracts']['wire']['plus_guid']) {
            return $wire; //not sent to plus
            return $wire; //not sent to plus
        }
        }
@@ -62,12 +59,45 @@ class Plus
        if (!$user) {
        if (!$user) {
            return $wire;
            return $wire;
        }
        }
        // error_log(var_export($wire->getTimestamp(), true));
        // check the users tier if passed in. If not, it's a standard monthly subscription.
        switch ($tier) {
            case 'lifetime':
                $user->setPlusExpires(9999999999); //life
                break;

            case 'yearly':
                $user->setPlusExpires($this->calculatePlusExpires('+1 year', $wire->getTimestamp(), $user->plus_expires));
                break;

            default:
                $user->setPlusExpires($this->calculatePlusExpires('+30 days', $wire->getTimestamp(), $user->plus_expires));
                break;
        }


        $user->setPlusExpires(strtotime('+30 days', $wire->getTimestamp()));
        $user->save();
        $user->save();


        //$wire->setSender($user);
        //$wire->setSender($user);
        return $wire;
        return $wire;
    }
    }


    /**
     * Calculates a user's plus expirey date - factoring in upgrades to existing subscriptions. 
     *
     * @param [String] $timespan - first param of strtotime().
     * @param [Integer] $wireTimestamp - the unix timestamp on the wire transaction. 
     * @param [Integer] $previousTimestamp - the users previous subscription unix timestamp.
     * @return [Integer] the new unix expiry date. 
     */
    public function calculatePlusExpires($timespan, $wireTimestamp, $previousTimestamp = null) {
        if ($previousTimestamp === 9999999999) {
            throw new \Exception('Already existing lifetime subscription');
        }

        if($previousTimestamp === null || $previousTimestamp < time()) {
            return strtotime($timespan, $wireTimestamp);
        }

        return strtotime($timespan, $previousTimestamp);
    }
}
}
+2 −1
Original line number Original line Diff line number Diff line
@@ -315,8 +315,9 @@ class Manager
            'description' => 'Wire',
            'description' => 'Wire',
            'user' => $wire->getReceiver(),
            'user' => $wire->getReceiver(),
        ]);*/
        ]);*/
        
        $this->plusDelegate
        $this->plusDelegate
            ->onWire($wire, $data['receiver_address'], $tier);
            ->onWire($wire, $data['receiver_address'], $this->tier);


        $this->sendNotification($wire);
        $this->sendNotification($wire);


+9 −1
Original line number Original line Diff line number Diff line
@@ -50,6 +50,10 @@ class PlusSpec extends ObjectBehavior


        $sender->setPlusExpires(Argument::any())
        $sender->setPlusExpires(Argument::any())
            ->shouldBeCalled();
            ->shouldBeCalled();

        $sender->get('plus_expires')
            ->willReturn(222);

        $sender->save()
        $sender->save()
            ->shouldBeCalled();
            ->shouldBeCalled();


@@ -124,6 +128,10 @@ class PlusSpec extends ObjectBehavior


        $sender->setPlusExpires(Argument::any())
        $sender->setPlusExpires(Argument::any())
            ->shouldBeCalled();
            ->shouldBeCalled();

        $sender->get('plus_expires')
            ->willReturn(222);

        $sender->save()
        $sender->save()
            ->shouldBeCalled();
            ->shouldBeCalled();