/**
  * @param $limit null|int
  * @return FeedModel[]
  */
 public function getFeedModels($limit = null)
 {
     $query = $this->hasMany(FeedModel::className(), ['user' => 'id']);
     if ($limit) {
         $query->limit($limit);
     }
     return $query->all();
 }
 public function run()
 {
     $feedId = \Yii::$app->request->get('feed_id');
     /** @var FeedModel $feed */
     $feed = FeedModel::findOne($feedId);
     if ($feed && $feed->user == \Yii::$app->user->identity->id) {
         NewModel::updateAll(['read' => 1], ['feed' => $feedId, 'read' => 0]);
         \Yii::$app->cache->delete($this->getNewsCacheKey($feedId));
         \Yii::$app->cache->delete($this->getFeedsCacheKey());
     }
     return $this->controller->redirect(['/user/news/list', 'feed_id' => $feedId, 'page' => 1]);
 }
 public function run()
 {
     $feedId = \Yii::$app->request->get('feed_id');
     /** @var FeedModel $feed */
     $feed = FeedModel::findOne($feedId);
     if ($feed && $feed->user == \Yii::$app->user->identity->id) {
         NewModel::deleteAll(['feed' => $feedId]);
         $feed->delete();
         $iconPath = FeedModel::getIconPath($feed->id);
         if (file_exists($iconPath)) {
             unlink($iconPath);
         }
         \Yii::$app->cache->delete($this->getNewsCacheKey($feedId));
         \Yii::$app->cache->delete($this->getFeedsCacheKey());
     }
     return $this->controller->redirect(['/user/news/list', 'feed_id' => '', 'page' => 1]);
 }
 private function getCurrentFeed()
 {
     if (!$this->currentFeed) {
         if ($feedId = \Yii::$app->request->get('feed_id')) {
             $this->currentFeed = FeedModel::findOne($feedId);
         } else {
             /** @var UserModel $userModel */
             $userModel = \Yii::$app->user->identity;
             $userFirstFeed = $userModel->getFeedModels(1);
             $this->currentFeed = $userFirstFeed ? $userFirstFeed[0] : null;
         }
     }
     return $this->currentFeed;
 }
 /**
  * @return FeedModel
  */
 public function getFeedModel()
 {
     return $this->hasOne(FeedModel::className(), ['id' => 'feed'])->one();
 }
 private function saveIcon()
 {
     try {
         $urlData = parse_url($this->url);
         $siteUrl = $urlData['scheme'] . '://' . $urlData['host'];
         $html = (new Client())->createRequest()->setUrl($siteUrl)->send()->getContent();
         $saw = new \nokogiri($html);
         $icons = $saw->get('link[rel="icon"]')->toArray();
         $iconUrl = null;
         if ($icons) {
             $iconUrl = $icons[0]['href'];
             $iconUrl = strstr($iconUrl, 'http') === false ? $siteUrl . $iconUrl : $iconUrl;
         } else {
             $iconUrl = $siteUrl . '/favicon.ico';
         }
         $response = (new Client())->createRequest()->setUrl($iconUrl)->send();
         if ($response->isOk) {
             file_put_contents(FeedModel::getIconsPath() . '/' . $this->id . '.' . pathinfo($iconUrl, PATHINFO_EXTENSION), $response->getContent());
         }
     } catch (\Exception $e) {
     }
     return true;
 }