SubscriptionQueryHandler.php 1.16 KB
<?php

namespace FootyRoom\Queries\Subscription;

use Illuminate\Database\Connection;

class SubscriptionQueryHandler
{
    /**
     * @var \Illuminate\Database\Connection
     */
    protected $mysql;

    /**
     * Constructor.
     *
     * @param \Illuminate\Database\Connection $mysql
     */
    public function __construct(Connection $mysql)
    {
        $this->mysql = $mysql;
    }

    /**
     * Find subscription.
     *
     * @param \FootyRoom\Queries\Subscription\OneSubscriptionQuery $query
     *
     * @return object
     */
    public function findOne(OneSubscriptionQuery $query)
    {
        $builder = $this->mysql

        ->table('subscriptions');

        if ($query->getUserId()) {
            $builder->where('user_id', '=', $query->getUserId());
        }

        if ($query->getSubjectType()) {
            $builder->where('subject_type', '=', $query->getSubjectType());
        }

        if ($query->getSubjectId()) {
            $builder->where('subject_id', '=', $query->getSubjectId());
        }

        $subscription = $builder->first();

        if (!$subscription) {
            return null;
        }

        return $subscription;
    }
}