/**
  * Fetches all timelice pictures from the API and stores it locally.
  * @param int $limit
  * @throws ValidationException
  * @throws null
  */
 public function syncPics($limit = 100)
 {
     // Get folder for image files
     $picDir = Folder::find_or_make(Config::inst()->get('FacebookAPI', 'pic_directory'));
     // Get pics and create records
     foreach ($this->requestTimelinePics($limit, true) as $pic) {
         if (!($p = FacebookTimelinePic::get()->filter('UID', $pic->ID)->first())) {
             $p = new FacebookTimelinePic();
             // Get image name from URI
             $imgName = explode('?', basename($pic->Source), 2)[0];
             // Save image to files
             file_put_contents('../' . $picDir->Filename . $imgName, file_get_contents($pic->Source));
             // Generate
             $p->UID = $pic->ID;
             $p->Caption = $pic->Name;
             $p->Likes = $pic->Likes;
             $p->Date = $pic->Date;
             $p->Name = $imgName;
             $p->Filename = $picDir->Filename . $imgName;
             $p->Title = $imgName;
             $p->ParentID = $picDir->ID;
             $p->OwnerID = Member::currentUserID();
             $p->write();
             // Check if there was an existing wall post entry for the pic
             if ($post = FacebookPost::get()->filter('UID', $p->UID)->first()) {
                 if ($post->PicID == 0) {
                     $post->PicID = $p->ID;
                     $post->write();
                 }
             }
         } else {
             $p->Likes = $pic->Likes;
             $p->write();
         }
     }
 }