Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.74 KB | None | 0 0
  1. <?php
  2.  
  3. require 'vendor/autoload.php';
  4.  
  5. class TagChecker
  6. {
  7.     const dev_token = '5356430951.6775add.248985fd3f0a475a939f5851245491a7';
  8.  
  9.     private $guzzle;
  10.     private $tag;
  11.  
  12.     public function __construct(string $tag, $token = null)
  13.     {
  14.         $this->tag = $tag;
  15.  
  16.         $this->guzzle = new \GuzzleHttp\Client(
  17.             ['base_uri' => 'https://api.instagram.com/v1/tags/']
  18.         );
  19.  
  20.         if ($token) {
  21.             $this->token = $token;
  22.         } else {
  23.             $this->token = self::dev_token;
  24.         }
  25.     }
  26.  
  27.     public function handle() : string
  28.     {
  29.         $data = $this->getTagInfoFromInstagram();
  30.         $uploaders = $this->getUploadersInfo($data);
  31.  
  32.         return json_encode($uploaders);
  33.     }
  34.  
  35.     private function sendRequest(string $method, string $endpoint, array $headers = []) : stdClass
  36.     {
  37.         $request = $this->guzzle->request($method, $endpoint, $headers);
  38.         return json_decode($request->getBody()->getContents());
  39.     }
  40.  
  41.     private function getTagInfoFromInstagram() : array
  42.     {
  43.         $url = $this->tag . '/media/recent?access_token=' . $this->token;
  44.         return $this->sendRequest('GET', $url)->data;
  45.     }
  46.  
  47.     private function getUploadersInfo(array $data) : stdClass
  48.     {
  49.         $response = new stdClass();
  50.         $response->inserted = 0;
  51.  
  52.         foreach ($data as $row) {
  53.  
  54.             $uploader = new TagModelUser();
  55.             $uploader
  56.                 ->setTag($this->tag)
  57.                 ->setUserId($row->user->id)
  58.                 ->setUsername($row->user->username)
  59.                 ->setFullName($row->user->full_name);
  60.             $result = $uploader->save();
  61.  
  62.             if ($result) {
  63.                 $response->inserted += 1;
  64.             }
  65.         }
  66.  
  67.         $tagModelUser = new TagModelUser();
  68.  
  69.         $response->uploaders = $tagModelUser->count($this->tag);
  70.  
  71.         return $response;
  72.     }
  73. }
  74.  
  75. class TagModelUser extends TagModel
  76. {
  77.     public function getTag() : string
  78.     {
  79.         return $this->tag;
  80.     }
  81.  
  82.     public function setTag($tag) : self
  83.     {
  84.         $this->tag = $tag;
  85.         return $this;
  86.     }
  87.  
  88.     public function getUserId() : int
  89.     {
  90.         return $this->userId;
  91.     }
  92.  
  93.     public function setUserId(int $userId) : self
  94.     {
  95.         $this->userId = $userId;
  96.         return $this;
  97.     }
  98.  
  99.     public function getUsername() : string
  100.     {
  101.         return $this->username;
  102.     }
  103.  
  104.     public function setUsername(string $username) : self
  105.     {
  106.         $this->username = $username;
  107.         return $this;
  108.     }
  109.  
  110.     public function getFullName() : string
  111.     {
  112.         return $this->fullName;
  113.     }
  114.  
  115.     public function setFullName(string $fullName) : self
  116.     {
  117.         $this->fullName = $fullName;
  118.         return $this;
  119.     }
  120.  
  121.     public function save() : bool
  122.     {
  123.         if(!$this->getTag()) {
  124.             echo 'Tag cannot be empty!';
  125.             return false;
  126.         }
  127.  
  128.         if (!$this->getUserId()) {
  129.             echo 'User ID cannot be empty!';
  130.             return false;
  131.         }
  132.  
  133.         if (!$this->getUsername()) {
  134.             echo 'Username cannot be empty!';
  135.             return false;
  136.         }
  137.  
  138.         if (!$this->getFullName()) {
  139.             echo 'Full name cannot be empty!';
  140.             return false;
  141.         }
  142.  
  143.         return parent::save();
  144.     }
  145. }
  146.  
  147. class TagModel
  148. {
  149.  
  150.     const db_name = 'instagram_api';
  151.     const db_host = 'localhost';
  152.     const db_username = 'root';
  153.     const db_password = '';
  154.  
  155.     private $db;
  156.  
  157.     protected $tag;
  158.     protected $userId;
  159.     protected $username;
  160.     protected $fullName;
  161.  
  162.     public function __construct()
  163.     {
  164.         $dsn = 'mysql:dbname=' . self::db_name . ';host=' . self::db_host;
  165.         $this->db = new PDO($dsn, self::db_username, self::db_password);
  166.         $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  167.         $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  168.     }
  169.  
  170.     public function save() : bool
  171.     {
  172.         $tagId = $this->getTagId($this->tag);
  173.         $user = $this->checkIfUserExists($this->userId, $tagId);
  174.         if (!$user) {
  175.             return $this->insertUser($tagId);
  176.         }
  177.         return false;
  178.     }
  179.  
  180.     public function count($tag) : int
  181.     {
  182.         $tagId = $this->getTagId($tag);
  183.         $sql = "SELECT COUNT(id) FROM users WHERE users.tagId = $tagId";
  184.         return $this->db->query($sql)->fetchColumn();
  185.     }
  186.  
  187.     public function getTagId(string $tag) : int
  188.     {
  189.         $sql = "SELECT id FROM tags WHERE tags.name = '$tag'";
  190.         $result = $this->db->query($sql)->fetchColumn();
  191.  
  192.         if ($result) {
  193.             return $result;
  194.         }
  195.  
  196.         $id = $this->insertTag($tag);
  197.         return $id;
  198.     }
  199.  
  200.     private function insertTag(string $tag) : int
  201.     {
  202.         $sql = "INSERT INTO tags (`name`) VALUES (?)";
  203.         $query = $this->db->prepare($sql);
  204.         $result = $query->execute([$tag]);
  205.  
  206.         return $this->db->lastInsertId();
  207.     }
  208.  
  209.     private function checkIfUserExists(int $userId, int $tagId) : bool
  210.     {
  211.         $sql = "SELECT id FROM users WHERE users.userId = $userId AND users.tagId = $tagId";
  212.         $result = $this->db->query($sql)->fetchColumn();
  213.  
  214.         return $result ? true : false;
  215.     }
  216.  
  217.     private function insertUser($tag) : bool
  218.     {
  219.         $sql = "INSERT INTO users (`userId`, `username`, `fullname`, `tagId`) VALUES (?, ?, ?, ?)";
  220.         $query = $this->db->prepare($sql);
  221.         $result = $query->execute([
  222.             $this->userId, $this->username, $this->fullName, $tag
  223.         ]);
  224.  
  225.         return $result;
  226.     }
  227. }
  228.  
  229. $tagChecker = new TagChecker('TasteTheDR');
  230. header('Content-Type: application/json;charset=utf8');
  231. echo $tagChecker->handle();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement