<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Unset_file_listener implements SplObserver
{

    public function update(SplSubject $subject)
    {
        $event_data = $subject->get_event_data();
        if ($subject->get_event_name() == 'delete' && $event_data['affected_rows'] > 0) {
            $ci = &get_instance();
            $ci->load->config('callme/captcha');
            foreach ($event_data['records'] as $file) {
                $this->remove($ci->config->item('captcha_config')['img_path'] . $file['filename']);
            }
        }
    }

    protected function remove($file = '')
    {
        if (is_dir($file) || !file_exists($file)) {

            return true;
        }

        return unlink($file);
    }

}
